我怎么知道线程工作完成了?

Far*_*iba 5 java multithreading

在B类中,我怎么知道线程的工作完成了?在属性之后,一些工人正在运行.在B班,我需要知道工人是否完成了?

public class A implements InitializingBean{
     public void method1(){
        ...
    }    
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.print("test after properties set");      
        // send threads to executorService
        ExecutorService executorService = Executors
                .newFixedThreadPool(4);
        for (int i = 0; i < 4; i++) {
            Worker worker = new Worker();       
            executorService.submit(worker);
        }
    }
}
public class Worker implements Callable<Void>{
    @Override       
    public void call(){
     ...
   }
}
public class B{
   public void methodB(){
      A a = new A();
     a.method1();
     ///Here How can i know the job of the workers are finished?
   }
}
Run Code Online (Sandbox Code Playgroud)

Eze*_*iel 3

您可以为您的线程使用 Future 实现。它提供了一个 Future#isDone()

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html#isDone()