Java 如何通知另一个线程

Java 如何通知另一个线程,java,multithreading,Java,Multithreading,我想知道通知其他线程的最佳方式。例如,我有一个背景线程: public void StartBackgroundThread(){ new Thread(new Runnable() { @Override public void run() { //Do something big... //THEN HOW TO NOTIFY MAIN THREAD? } }).star

我想知道通知其他线程的最佳方式。例如,我有一个背景线程:

public void StartBackgroundThread(){
new Thread(new Runnable() {         
        @Override
        public void run() {
            //Do something big...
            //THEN HOW TO NOTIFY MAIN THREAD?
        }
    }).start();
}
当它完成时,它必须通知主线程吗?如果有人知道最好的方法,我会很感激的

只需调用notify()


一个线程通知另一个线程不是一个好方法。最好有一个主线程,让从线程工作。从线程始终在运行并等待,直到收到工作。我建议您绘制两列,并确定每个线程需要等待的确切位置

    public void run() 
    {
    //Do something big...
    synchronized(this)
    {
        done = true;
    }
    }
Java包含了一些库,这些库使这一过程变得非常简单,请参见
ExecutorService
和下面的文章
典型的答案是
阻塞队列。
BackgroundThread
(通常称为Producer)和
MainThread
(通常称为Consumer)都共享队列的一个实例(可能是在实例化时得到的)
BackgroundThread
calls
queue.put(message)
每次它有一个新消息和
MainThread
calls'队列时。take()
将阻止它,直到有消息接收为止。你可以喜欢超时和偷看,但通常人们想要一个
BlockingQueue
实例,比如
ArrayBlockingQueue`。

你不能“通知主线程”

最好的方法是使用,例如:

 import java.util.concurrent.*;

 // in main thread
 ExecutorService executorService = Executors.newSingleThreadExecutor();

 Future<?> future = executorService.submit(new Runnable() {         
    @Override
    public void run() {
        //Do something big...
    }
});

future.get(); // blocks until the Runnable finishes
import java.util.concurrent.*;
//主线程中
ExecutorService ExecutorService=Executors.newSingleThreadExecutor();
Future=executorService.submit(新Runnable(){
@凌驾
公开募捐{
//做点大事。。。
}
});
future.get();//阻塞直到Runnable完成
这些类是专门为处理异步操作而编写的,其中的所有代码都是为您编写的

编辑 如果不想在等待时阻止主线程,请在另一个线程内等待:

 final Future<?> future = executorService.submit(new Runnable() {         
    @Override
    public void run() {
        //Do something big...
    }
});

new Thread(new Runnable() {         
    @Override
    public void run() {
        future.get(); // blocks until the other Runnable finishes
        // Do something after the other runnable completes
    }
}).start();
final Future=executorService.submit(新的Runnable(){
@凌驾
公开募捐{
//做点大事。。。
}
});
新线程(新Runnable(){
@凌驾
公开募捐{
future.get();//阻塞,直到其他Runnable完成
//在其他runnable完成后执行某些操作
}
}).start();

完全根据您的问题,您可以这样做:

public class test
{
  Object syncObj = new Object();

  public static void main(String args[])
  {
    new test();
  }

  public test()
  {
    startBackgroundThread();

    System.out.println("Main thread waiting...");
    try
    {
      synchronized(syncObj)
      {
        syncObj.wait();
      }
    }
    catch(InterruptedException ie) { }
    System.out.println("Main thread exiting...");
  }

  public void startBackgroundThread()
  {
    (new Thread(new Runnable()
    {
      @Override
      public void run()
      {
        //Do something big...
        System.out.println("Background Thread doing something big...");
        //THEN HOW TO NOTIFY MAIN THREAD?
        synchronized(syncObj)
        {
          System.out.println("Background Thread notifing...");
      syncObj.notify();
        }
        System.out.println("Background Thread exiting...");
      }
    })).start();
  }
}
看到这个输出了吗

PS C:\Users\java> javac test.java
PS C:\Users\java> java test
Main thread waiting...
Background Thread doing something big...
Background Thread notifing...
Background Thread exiting...
Main thread exiting...

考虑阅读以下内容:它包括通知。(特别是第7页)当第二个线程执行“大任务”时,主线程将执行什么操作?它只会在第二个线程上等待吗?如果是这样,为什么还需要第二个线程呢?如果没有,你需要多说一点它将要做什么。例如,主线程将呈现GUI…你能告诉我为什么我们不能通知主线程吗?我很想知道,但是如果我想在主线程中的另一个runnable完成后做些什么呢?用您需要做的任何事情替换我答案中代码中的注释
//在另一个runnable完成后做些什么
PS C:\Users\java> javac test.java
PS C:\Users\java> java test
Main thread waiting...
Background Thread doing something big...
Background Thread notifing...
Background Thread exiting...
Main thread exiting...