定长线程池Demo

 1 import java.util.concurrent.ExecutorService;
 2 import java.util.concurrent.Executors;
 3 
 4 /**
 5  * Description:定长线程池Demo
 6  *
 7  * @author baozikengutou
 8  */
 9 public class Test2 {
10     
11     public static void main(String[] args) {
12         
13         // 创建固定长度线程池
14         ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4);
15         for (int i = 1; i < 41; i++) {
16             final int index = i;
17             fixedThreadPool.execute(new Runnable() {
18                 
19                 @Override
20                 public void run() {
21                     try {
22                         System.out.println("================>线程:" + index + "\n");
23                         Thread.sleep(1000 * 3);
24                     } catch (InterruptedException e) {
25                         e.printStackTrace();
26                     }
27                 }
28             });
29         }
30     }
31 }

 输出结果:

================>线程:1

================>线程:4

================>线程:3

================>线程:2

================>线程:5

================>线程:8

================>线程:7

================>线程:6

================>线程:9

================>线程:10

================>线程:11

================>线程:12

================>线程:13

================>线程:15

================>线程:14

================>线程:16

================>线程:17

================>线程:19

================>线程:18

================>线程:20

================>线程:21

================>线程:23

================>线程:24

================>线程:22

...

posted @ 2017-08-26 14:25  降龙-伏虎  阅读(683)  评论(0编辑  收藏  举报