读书人

java 主线程等候子线程执行完成后再执

发布时间: 2012-10-20 14:12:48 作者: rapoo

java 主线程等待子线程执行完成后再执行

java 主线程等候子线程执行完成后再执行await: 会阻塞等待计数器减少到0位置. 带参数的await是多了等待时间.countDown: 将当前的技术减1getCount(): 返回当前的计数显而易见, 我们只需要在子线程执行之前, 赋予初始化countDownLanch, 并赋予线程数量为初始值.每个线程执行完毕的时候, 就countDown一下.主线程只需要调用await方法, 可以等待所有子线程执行结束, 看代码:
public?class?Threads {
//???static ExecutorService executorService = Executors.newFixedThreadPool(1);?????static?final?BlockingQueue<Integer>?queue?=?new?ArrayBlockingQueue<Integer>(1);?????public?static?void?main(String[]?args)?throws?InterruptedException,?ExecutionException {??????????int?threads?=?5;??????????CountDownLatch countDownLatch?=?new?CountDownLatch(threads);??????????for(int?i=0;i<threads;i++){??????????????SubThread thread?=?new?SubThread(2000*(i+1),?countDownLatch);??????????????thread.start();??????????}//????????Future future = executorService.submit(thread);??????????mainThreadOtherWork();??????????System.out.println(“now waiting sub thread done.”);//????????future.get();//????????queue.take();??????????countDownLatch.await();//????????try {//????????????thread.join();//????????} catch (InterruptedException e) {//????????????e.printStackTrace();//????????}??????????System.out.println(“now all done.”);//????????executorService.shutdown();?????}
?????private?static?void?mainThreadOtherWork() {??????????System.out.println(“main thread work start”);??????????try?{??????????????Thread.sleep(3000L);??????????}?catch?(InterruptedException e) {??????????????e.printStackTrace();??????????}??????????System.out.println(“main thread work done.”);?????}
?????public?static?class?SubThread?extends?Thread{??????????//????????private BlockingQueue<Integer> queue;??????????private?CountDownLatch?countDownLatch;??????????private?long?work;????????????????????/**?????????? *?@param?queue?????????? *///????????public SubThread(BlockingQueue<Integer> queue) {//????????????this.queue = queue;//????????????this.work = 5000L;//????????}????????????????????public?SubThread(long?work,?CountDownLatch?countDownLatch) {//????????????this.queue = queue;??????????????this.countDownLatch?=?countDownLatch;??????????????this.work?=?work;??????????}
??????????@Override??????????public?void?run() {??????????????try{??????????????working();??????????????}finally{//?????????????????try {//??????????????????????queue.put(1);//?????????????????} catch (InterruptedException e) {//??????????????????????e.printStackTrace();//?????????????????}???????????????????countDownLatch.countDown();??????????????}????????????????????????}
??????????private?void?working() {??????????????System.out.println(getName()+” sub thread start working.”);??????????????busy();??????????????System.out.println(getName()+” sub thread stop working.”);??????????}
??????????private?void?busy() {??????????????try?{???????????????????sleep(work);??????????????}?catch?(InterruptedException e) {???????????????????e.printStackTrace();??????????????}??????????}???????????????}}此种方法也适用于使用 ExecutorService summit 的任务的执行.另外还有一个并发包的类CyclicBarrier, 这个是(子)线程之间的互相等待的利器. 栅栏, 就是把大家都在一个地方堵住, 就像水闸, 等大家都完成了之前的操作, 在一起继续下面的操作. 不过就不再本篇的讨论访问内了.EOF

读书人网 >编程

热点推荐