package three.day.thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class CachedThreadPoolTest {
final Semaphore sp = new Semaphore(3);
for(int i=0;i<3;i++){
Runnable runnable = new Runnable(){
public void run(){
System.out.println("线程" + Thread.currentThread().getName() );
}
};
service.execute(runnable);
}
service.shutdown();
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class CachedThreadPoolTest {
public static void main(String[] args) {
//ExecutorService service = Executors.newFixedThreadPool(2);创建固定大小的线程池
//Executors.newSingleThreadExecutor();相当于Executors.newFixedThreadPool(1);的线程池
//而newCachedThreadPool();则是有空线程则重用,没得重用则新创建
ExecutorService service = Executors.newCachedThreadPool();final Semaphore sp = new Semaphore(3);
for(int i=0;i<3;i++){
Runnable runnable = new Runnable(){
public void run(){
System.out.println("线程" + Thread.currentThread().getName() );
}
};
service.execute(runnable);
}
service.shutdown();
}
}