简体   繁体   English

两个线程之间的通信

[英]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. ThreadA创建ThreadB并给B他的接口,以便ThreadB可以使用它。 When ThreadB uses the methods from the interface ThreadA can process the result. 当ThreadB使用接口中的方法时,ThreadA可以处理结果。 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. 例如,一个BlockingQueue。

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html 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. 当Java中的线程在另一个对象中执行方法时,无论该对象是在其自己的线程中还是在另一个线程中创建的,它将在其自己的线程中执行该方法。 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. 全局变量之间的内存在线程之间共享,因此,如果您在ThreadB中更改了ThreadA使用的变量,则这两个线程都会更改。 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. 如果要在线程之间传递消息,如另一个答案所述,则某种类型的Queue会很好。

Your example does not show any communication between threads. 您的示例未显示线程之间的任何通信。

A Thread is not a thread . 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 . Thread是一个Java对象,可用于创建( start() )和管理线程的生命周期。

When your ThreadB run method calls ti.processFinished(result) , that is not an interaction between threads. 当您的ThreadB运行方法调用ti.processFinished(result) ,这不是线程之间的交互。 the processFinished(...) call happens in the same thread that did the call. processFinished(...)调用发生在与调用相同的线程中。

No communication happens unless processFinished() updates some shared variable that the other thread will access. 除非processFinished()更新其他线程将访问的某些共享变量 ,否则不会发生通信。 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. 由于您没有向我们显示共享变量(或processFinished()实际执行的任何操作,或启动ThreadB后其他线程执行的操作),因此没有向我们显示任何通信。


FYI, one of the most versatile means of communicating between threads is to have them share an ArrayBlockingQueue . 仅供参考,线程之间通信的最通用方法之一就是让它们共享ArrayBlockingQueue One thread can put messages in the queue, and the other thread can get the messages from the queue and act on them. 一个线程可以将消息放入队列中,而另一个线程可以从队列中获取消息并对其执行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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