1. 程式人生 > 實用技巧 >央行數字貨幣研討會在深圳前海召開

央行數字貨幣研討會在深圳前海召開

我們來看一個簡單的執行緒池。

首先建立一個Runnable介面的實現類(當然也可以是Callable介面,我們上面也說過兩者的區別)

package com.study;

import java.util.Date;

public class MyRunnable implements Runnable{
    private String command;
    public MyRunnable(String s){
        this.command = s;
    }

    public void run() {
        System.out.println(Thread.currentThread().getName() 
+ " Start. Time = "+new Date()); processCommand(); System.out.println(Thread.currentThread().getName() + " End. Time = "+new Date()); } private void processCommand(){ try{ Thread.sleep(5000); }catch (InterruptedException e){ e.printStackTrace(); } } @Override
public String toString() { return this.command; } }

編寫測試程式,通過ThreadPoolExecutor建構函式自定義引數的方式來建立執行緒池。

package com.study;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorDemo {
    
//核心執行緒數 private static final int CORE_POOL_SIZE = 5; //最大執行緒數 private static final int MAX_POOL_SIZE = 10; // private static final int QUEUE_CAPACITY = 100; //等待時間 private static final Long KEEP_ALIVE_TIME = 1L; public static void main(String[] args) { ThreadPoolExecutor executor = new ThreadPoolExecutor( CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS,//等待時間單位為秒 new ArrayBlockingQueue<Runnable>(QUEUE_CAPACITY),//任務佇列 new ThreadPoolExecutor.CallerRunsPolicy());//飽和策略 for (int i = 0;i<10;i++){ Runnable worker = new MyRunnable(""+i); executor.execute(worker); } executor.shutdown(); while (executor.isTerminated()){ System.out.println("Finished all threads"); } } }

可以看到以上程式碼我們指定了:

1、corePoolSize:核心執行緒數為5.

2、maximumPoolSize:最大執行緒數10

3、keepAliveTime:等待時間為1L;

4、unit:等待時間的單位為TimeUnit.SECONDS

5、workQueue:任務對流為ArrayBlockingQueue,並且容量為100;

6、handler:飽和策略為CallerRunsPolicy

輸出:

pool-1-thread-3 Start. Time = Tue Oct 13 19:02:04 CST 2020
pool-1-thread-1 Start. Time = Tue Oct 13 19:02:04 CST 2020
pool-1-thread-5 Start. Time = Tue Oct 13 19:02:04 CST 2020
pool-1-thread-4 Start. Time = Tue Oct 13 19:02:04 CST 2020
pool-1-thread-2 Start. Time = Tue Oct 13 19:02:04 CST 2020
pool-1-thread-5 End. Time = Tue Oct 13 19:02:09 CST 2020
pool-1-thread-2 End. Time = Tue Oct 13 19:02:09 CST 2020
pool-1-thread-1 End. Time = Tue Oct 13 19:02:09 CST 2020
pool-1-thread-3 End. Time = Tue Oct 13 19:02:09 CST 2020
pool-1-thread-4 End. Time = Tue Oct 13 19:02:09 CST 2020
pool-1-thread-3 Start. Time = Tue Oct 13 19:02:09 CST 2020
pool-1-thread-4 Start. Time = Tue Oct 13 19:02:09 CST 2020
pool-1-thread-2 Start. Time = Tue Oct 13 19:02:09 CST 2020
pool-1-thread-5 Start. Time = Tue Oct 13 19:02:09 CST 2020
pool-1-thread-1 Start. Time = Tue Oct 13 19:02:09 CST 2020
pool-1-thread-2 End. Time = Tue Oct 13 19:02:14 CST 2020
pool-1-thread-3 End. Time = Tue Oct 13 19:02:14 CST 2020
pool-1-thread-5 End. Time = Tue Oct 13 19:02:14 CST 2020
pool-1-thread-1 End. Time = Tue Oct 13 19:02:14 CST 2020
pool-1-thread-4 End. Time = Tue Oct 13 19:02:14 CST 2020