开发者_开发知识库I have created a one child thread and now i want to send some message from child thread to main thread. how can i do this?
In the Thread you created, you will need a reference to the thread you are trying to send the message (method call) to.
I.E.
MainClass.java:
public class MainClass implements Runnable
{
private Queue<String> internalQueue;
private boolean keepRunning;
public MainClass()
{
keepRunning = true;
internalQueue = new Queue<String>();
}
public void queue(String s)
{
internalQueue.add(s);
this.notify();
}
public void run()
{
// main thread
// create child thread
Runnable r = new YourThread(this);
new Thread().start(r);
// process your queue
while (keepRunning) {
// check if there is something on your queue
// sleep
this.wait();
}
}
public static void main(String[] args)
{
MainClass mc = new MainClass();
mc.run();
}
}
YourThread.java
public class YourThread implements Runnable
{
private MainClass main;
public YourThread(MainClass c)
{
this.main = c;
}
public void run()
{
// your thread starts here
System.out.println("Queue a message to main thread");
main.queue("Hi from child!");
}
}
Use Callable
interface instead of Runnable
There is no parent-child relationship between threads.
A thread can spawn another thread, and once spawned, the 2 threads are independent from each other.
Please let us know what you mean by send a message. That can encompass a wide range of use cases, and each has its best implementations.
For example, if you want to synchronize 2 threads, you can go ahead and use a simple wait / notify mechanism. For this, you will have to share an object between the 2.
If you want to compute a value in the spawned thread and pass it back, then you can use queues. But you will have to also tell us more about how the execution of the 2 threads is related so that we can suggest the appropriate way to implement it. (Such as producer-consumer mechanism)
Used Priority queue as the object on which the parent (Main thread) and child thread communicate. Definition of the child runnable
class CommunicationThead implements Runnable{
Queue<String> commQueue=null;
CommunicationThead(Queue<String> q){
super();
this.commQueue=q;
}
public void run(){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("child :interuppted on sleep");
}
synchronized(commQueue){
if(commQueue!=null)
{
commQueue.add("Yo");
System.out.println("message added by child");
}
commQueue.notifyAll();
}
}
}
Calling the child runnable (called in the main()) the main thread waits till it receives a message from the child thread
Queue<String> q=new PriorityQueue<String>();
Thread child=new Thread(new CommunicationThead(q));
child.start();
boolean msgReceived=true;
while(msgReceived){
synchronized(q){
if(q.isEmpty())
{
try {
System.out.println("parent: queue empty | parent waiting");
q.wait(1000);
} catch (InterruptedException e) {
System.out.println("parent wait interrupted");
}
}
else{
System.out.println("parent found message :"+q.poll());
msgReceived=false;
}
}
}
public class MyClass {
private MyInterface delegate;
public void setDelegate(MyInterface delegate) {
this.delegate = delegate;
}
// this will be performed in a background thread
public void doStuff() {
Future<String> future = Executors.newSingleThreadExecutor().submit(new Callable<String>() {
@Override
public String call() throws Exception {
return "hello world";
}
});
delegate.handleResponse(future.get());
}
}
public interface MyInterface {
void handleResponse(String value);
}
public class MainClass implements MyInterface {
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.setDelegate(this);
myClass.doStuff();
}
@Override
public void handleResponse(String value) {
// this will be on the main thread
System.out.println(value);
}
}
精彩评论