当前位置:首页 > 开发 > 编程语言 > 多线程 > 正文

java线程

发表于: 2014-11-18   作者:chendaiming   来源:转载   浏览:
摘要: package test; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Test { //线程辅助类 private s
package test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {

        //线程辅助类
	private static CountDownLatch mLatch = new CountDownLatch(4);

	public static void main(String[] args) throws InterruptedException {
		//创建线程池,线程数3个
		final ExecutorService exec = Executors.newFixedThreadPool(3);
		for (int index = 0; index < 4; index++) {
			final int NO = index + 1;
			Runnable run = new Runnable() {
				public void run() {
					try {
						System.out.println(NO + " working");
						Thread.sleep(2000);
					} catch (InterruptedException e) {
					} finally {
						mLatch.countDown();
					}
				}
			};
			exec.submit(run);
		}
                //等待线程全部执行完毕,确保程序的流程
		mLatch.await();
		System.out.println("finish");
		exec.shutdown();
	}
}

java线程详解: http://chendaiming.iteye.com/blog/2158169

 

 

 

 

 

java线程

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号