Solved

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

Posted on 2009-07-11
6
585 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 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven project error 5 56
tomcat not starting 6 45
Custom Java Application connects to database: Which one of the 20? 4 29
Which non-HTML GUI front end to use with Java? 3 22
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 …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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 …

803 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