Solved

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

Posted on 2009-07-11
6
583 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now