定長線程池Demo,長線程池demo

來源:互聯網
上載者:User

定長線程池Demo,長線程池demo

 1 import java.util.concurrent.ExecutorService; 2 import java.util.concurrent.Executors; 3  4 /** 5  * Description: 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                 @Override20                 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

...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.