java5线程并发库的应用
//ExecutorService threadPool = Executors.newFixedThreadPool(3);
// ExecutorService threadPool = Executors.newCachedThreadPool();
ExecutorService threadPool = Executors.newSingleThreadExecutor();
for(int i=1;i<=10;i++){
final int task=i;
threadPool.execute(new Runnable() {
@Override
public void run() {
for(int j=1;j<=10;j++){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"is looping of "+j+"for task of "+task);
}
}
});
}
System.out.println("task have committed");
threadPool.shutdown();//执行完之后销毁线程
threadPool.shutdownNow();//执行到这里之后立刻销毁线程
Executors.newScheduledThreadPool(3).schedule(new Runnable() {
@Override
public void run() {
System.out.println("booming");
}
}, 1, TimeUnit.SECONDS);
如有疑问,请发邮件:1176306419@qq.com
github: https://github.com/wangrui0/