简体   繁体   中英

Communication between two Threads

I need your opinion or an alternative solution on problem I've got. I have the following code:

ThreadA creates ThreadB and gives B his interface so ThreadB can use this. When ThreadB uses the methods from the interface ThreadA can process the result. Is this a correct way to handle communication? If not, how would it be correct?

public class ThreadA implements ThreadInterface {

    public ThreadA() {

        ThreadB b = new ThreadB((ThreadInterface) this);
        b.start();
    }

    @Override
    public void processFinished(int result) {
        // Do something with the result
    }

}


public interface ThreadInterface {

    void processFinished(int result);
}


public class ThreadB extends Thread {

    ThreadInterface ti;

    public ThreadB(ThreadInterface pTi) {
        ti = pTi;
    }

    @Override
    public void run() {
        int result = 0;

        // ... do things and save them into result
        ti.processFinished(result);
    }
}

Calling the method in the object is not the same as running code on that thread.

To communicate between two threads you should use some sort of message passing algorithm. For example a BlockingQueue.

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html

When a thread in Java executes a method in another object, whether that object was made in its own or another thread it will execute it in its own thread. Memory between global variables are shared between threads so if you change a variable in ThreadB that ThreadA uses, it will change for both threads. What you're doing is an organized way of doing that, so there's really no difference. If you want to have messages passed between threads, as the other answer has stated, some type of Queue would be good.

Your example does not show any communication between threads.

A Thread is not a thread . A thread is an independent execution of your code. A Thread is a Java object that can be used to create ( start() ) and manage the life cycle of a thread .

When your ThreadB run method calls ti.processFinished(result) , that is not an interaction between threads. the processFinished(...) call happens in the same thread that did the call.

No communication happens unless processFinished() updates some shared variable that the other thread will access. Since you haven't shown us the shared variable (or anything that processFinished() actually does, or what the other thread does after starting ThreadB) you haven't shown us any communication happening.


FYI, one of the most versatile means of communicating between threads is to have them share an ArrayBlockingQueue . One thread can put messages in the queue, and the other thread can get the messages from the queue and act on them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM