public class Test {
private static int CORE_POOL_SIZE=4;
private static final int MAX_POOL_SIZE = 100;
private static final int BLOCKING_QUEUE_SIZE = 10000;
private static final String MULTI_THREAD_NAMING_PATTERN = "";
private static final ExecutorService threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE,
0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(BLOCKING_QUEUE_SIZE),
new BasicThreadFactory.Builder().namingPattern(MULTI_THREAD_NAMING_PATTERN).daemon(true).build(),
new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) {
//固定了线程池中的线程。
//ExecutorService threadPool = Executors.newFixedThreadPool(5);
//缓存型池子,动态的增加减少线程
//ExecutorService threadPool = Executors.newCachedThreadPool();
//单一线程池
//ExecutorService threadPool = Executors.newSingleThreadExecutor();
//调度型线程池
//ExecutorService threadPool = Executors.newScheduledThreadPool(3);
for(int i=1;i<=10;i++){
final int task = i;
threadPool.execute(new Runnable() {
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+
" is looping of "+1+" for task of "+task);
/* for(int j=1;j<=5;j++){
System.out.println(Thread.currentThread().getName()+
" is looping of "+j+" for task of "+task);
}*/
}
});
}
System.out.println("all of 10 tasks has committed");
threadPool.shutdown();
}
}
线程池 ExecuterService 的用法
最新推荐文章于 2023-09-14 23:51:21 发布