欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java 线程池,

来源: javaer 分享于  点击 35217 次 点评:150

java 线程池,



package test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestThreadPool {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

ThreadPoolExecutor threadPoolExcExecutor = new ThreadPoolExecutor(10,20,0,TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(3),new ThreadPoolExecutor.CallerRunsPolicy());
for(int i=1;i<=5;i++){
threadPoolExcExecutor.execute(new TestThread("Thread-"+i));
}
System.out.println("All jobs done.");

//调用shutdown方法线程池将关闭,不再接受新任务,但之前添加的任务将会继续执行直接结束.
//如果不shutdown,线程池会一直存在.
threadPoolExcExecutor.shutdown();
}

}

class TestThread implements Runnable{

String threadName;
public TestThread(String threadName){
this.threadName = threadName;
}

@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("I'm "+threadName+", I'm running ! ");
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
System.out.println(threadName+" has awake ! ");
}
}


相关文章

    暂无相关文章
相关栏目:

用户点评