Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-07-11
6
Medium Priority
?
593 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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

824 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