ExecutorService 用例
Posted 心想事成
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ExecutorService 用例相关的知识,希望对你有一定的参考价值。
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class TestCachedThreadPool{
- public static void main(String[] args){
- ExecutorService executorService = Executors.newCachedThreadPool();
- // ExecutorService executorService = Executors.newFixedThreadPool(5);
- // ExecutorService executorService = Executors.newSingleThreadExecutor();
- for (int i = 0; i < 5; i++){
- executorService.execute(new TestRunnable());
- System.out.println("************* a" + i + " *************");
- }
- executorService.shutdown();
- }
- }
- class TestRunnable implements Runnable{
- public void run(){
- System.out.println(Thread.currentThread().getName() + "线程被调用了。");
- }
- }
以上是关于ExecutorService 用例的主要内容,如果未能解决你的问题,请参考以下文章
ExecutorService中submit和execute区别
多线程ExecutorService中submit和execute区别
ExecutorService中submit()和execute()的区别