?
Solved

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

Posted on 2009-07-11
6
Medium Priority
?
591 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 135 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 90 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 135 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Accepted Solution

by:
guneshraj earned 150 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

719 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