1 import java.util.concurrent.ExecutorService;
 2 import java.util.concurrent.Executors;
 3 
 4 public class ThreadPoolTest
 5 {
 6     public static void main(String[] args)
 7     {
 8         ExecutorService threadPool = Executors.newFixedThreadPool(3);
 9         
10         for(int i = 0; i < 10; i++)
11         {
12             final String taskName = "task " + i;
13             threadPool.execute(new Runnable()
14             {
15                 public void run()
16                 {
17                     System.out.println(Thread.currentThread().getName() + "-" + taskName);
18                 }
19             });
20         }
threadPool.shutdown();
21 } 22 }

输出结果:

pool-1-thread-2-task 1
pool-1-thread-1-task 0
pool-1-thread-3-task 2
pool-1-thread-2-task 3
pool-1-thread-1-task 4
pool-1-thread-2-task 6
pool-1-thread-3-task 5
pool-1-thread-2-task 8
pool-1-thread-1-task 7
pool-1-thread-3-task 9

相关文章:

  • 2022-12-23
  • 2021-12-19
  • 2021-12-13
  • 2021-11-20
猜你喜欢
  • 2021-11-29
  • 2022-12-23
  • 2022-02-06
  • 2021-08-10
  • 2021-11-20
  • 2021-08-31
  • 2022-01-02
相关资源
相似解决方案