如何知道其他线程是否已经完成?我有一个对象,它的方法名为StartDownload()启动三个线程。当每个线程完成执行时,如何获得通知?是否有一种方法可以知道线程的一个(或全部)是否已经完成或仍在执行?
3 回答
守着星空守着你
TA贡献1799条经验 获得超8个赞
java.util.concurrent
java.util.concurrent.*
join()
Runnable
public interface CallbackRunnable extends Runnable { public void callback();}
public class CallbackExecutor implements Executor { @Override public void execute(final Runnable r) { final Thread runner = new Thread(r); runner.start(); if ( r instanceof CallbackRunnable ) { // create a thread to perform the callback Thread callerbacker = new Thread(new Runnable() { @Override public void run() { try { // block until the running thread is done runner.join(); ((CallbackRunnable)r).callback(); } catch ( InterruptedException e ) { // someone doesn't want us running. ok, maybe we give up. } } }); callerbacker.start(); } }}
CallbackRunnable
public void uncaughtException(Throwable e);
java.util.concurrent.Callable
java.util.concurrent
添加回答
举报
0/150
提交
取消