package Exce;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolTest {
/**
* @param args
*/
public static void main(String[] args) {
//线程池中放了三个线程,固定线程数
//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(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" the looping of "+j+" for task of "+task);
}
}
});
}//for
threadPool.shutdown();
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolTest {
/**
* @param args
*/
public static void main(String[] args) {
//线程池中放了三个线程,固定线程数
//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(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" the looping of "+j+" for task of "+task);
}
}
});
}//for
threadPool.shutdown();
}
}