一个简单的线程池Demo:Runnable + ThreadPoolExecutor

一个简单的线程池Demo:Runnable + ThreadPoolExecutor

首先创建一个简单的Runnable类

MyRunnable.java

import java.util.Date;

/**
 * 这是一个简单的Runnable类,需要大约5秒钟来执行其任务
 *
 *
 */
public class MyRunnable implements Runnable{
    
    private String command;
    public MyRunnable(String s){
        this.command = s;
    }

    @Override
    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构造自定义参数的方式来创建线程池

ThreadPoolExecutor.java

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构造函数自定义参数创建
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                CORE_POOL_SIZE,
                MAX_POOL_SIZE,
                KEEP_ALIVE_TIME,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(QUEUE_CAPACITY),
                new ThreadPoolExecutor.CallerRunsPolicy());

        for (int i = 0;i < 10; i++){
            //创建WorkerThread对象(WorkerThread类实现了 Runnable 接口)
            Runnable worker = new MyRunnable("" + i);
            //执行Runnable
            executor.execute(worker);
        }
        //终止线程池
        executor.shutdown();
        while (!executor.isTerminated()){

        }
        System.out.println("Finished all threads");
    }
}

执行结果:

pool-1-thread-5Start.Time = Mon Jun 01 15:34:06 CST 2020
pool-1-thread-2 Start.Time = Mon Jun 01 15:42:39 CST 2020
pool-1-thread-3 Start.Time = Mon Jun 01 15:42:39 CST 2020
pool-1-thread-5 Start.Time = Mon Jun 01 15:42:39 CST 2020
pool-1-thread-4 Start.Time = Mon Jun 01 15:42:39 CST 2020
pool-1-thread-1 Start.Time = Mon Jun 01 15:42:39 CST 2020
pool-1-thread-4 End.Time = Mon Jun 01 15:42:44 CST 2020
pool-1-thread-2 End.Time = Mon Jun 01 15:42:44 CST 2020
pool-1-thread-4 Start.Time = Mon Jun 01 15:42:44 CST 2020
pool-1-thread-2 Start.Time = Mon Jun 01 15:42:44 CST 2020
pool-1-thread-1 End.Time = Mon Jun 01 15:42:44 CST 2020
pool-1-thread-1 Start.Time = Mon Jun 01 15:42:44 CST 2020
pool-1-thread-3 End.Time = Mon Jun 01 15:42:44 CST 2020
pool-1-thread-5 End.Time = Mon Jun 01 15:42:44 CST 2020
pool-1-thread-3 Start.Time = Mon Jun 01 15:42:44 CST 2020
pool-1-thread-5 Start.Time = Mon Jun 01 15:42:44 CST 2020
pool-1-thread-5 End.Time = Mon Jun 01 15:42:49 CST 2020
pool-1-thread-3 End.Time = Mon Jun 01 15:42:49 CST 2020
pool-1-thread-1 End.Time = Mon Jun 01 15:42:49 CST 2020
pool-1-thread-2 End.Time = Mon Jun 01 15:42:49 CST 2020
pool-1-thread-4 End.Time = Mon Jun 01 15:42:49 CST 2020
Finished all threads

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值