Solved

How a static (util) class can pass data into Thread (Runnable)

Posted on 2009-07-11
6
589 Views
Last Modified: 2012-06-27
Dear experts,
Could you advice me how a Java static method, cocan pass data in a Thread class (Runnable)? It will be better with sample code.
Thanks in advance.
0
Comment
Question by:isuhendro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 45 total points
ID: 24831531
You can call the static method from the run() method of the thread
without creating any instance of it.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 30 total points
ID: 24831536
You will need to make a Thread subclass or a class that implments Runnable. Let's call that 'X'
public static void passData(X x, Object data) {
    x.setData(data);
}

Open in new window

0
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 45 total points
ID: 24831548
Something like:


A.java

public class A {
   public static void foo() {
     System.out.println("static method called.");
   }
}

B.java

public class B extends Thread {
   public void run() {
     A.foo(); // static method of A is called from thread.
   }
}
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 2

Accepted Solution

by:
guneshraj earned 50 total points
ID: 24834008
Threads are quite tricky to manage & I would suggest against working on this model If you attempt to pass data to a Thread or a running Thread, you need to do some checking.

The Thread model has changed since the early java days.

If you intend to pass data before the Thread Executes, then use the code from CEHJ & Ajay Singh before calling run()/Start().

If you would like to pass data when the thread is running, its best to create another class to store data or a event-listener-notifier framework.

Classes that uses this could be based from a Singleton class that would allow data to be sent & read from the Calling classes to the Java threads that are running.

This would be also useful to terminate a Thread from the Calling class (using events vars that are picked by the Thread)

Hope this helps.
0
 

Author Closing Comment

by:isuhendro
ID: 31602456
Thanks everyone!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24949137
:-)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question