线程池的使用简单示例

avatar 2020年12月16日18:31:08 0 110 views

直接上代码

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

/**
 * @author 言曌
 * @date 2020/12/16 6:22 下午
 */
/**
 * 业务线程池
 */
class BusinessThread implements Runnable {

    // 业务参数
    private String param1;
    private String param2;
    private String param3;

    public BusinessThread(String param1, String param2, String param3) {
        this.param1 = param1;
        this.param2 = param2;
        this.param3 = param3;
    }
    @Override
    public void run() {
        // TODO 业务逻辑
        System.out.println("Hello World!!!!!");
        System.out.println(param1);
        System.out.println(param2);
        System.out.println(param3);
    }


    public String getParam1() {
        return param1;
    }

    public void setParam1(String param1) {
        this.param1 = param1;
    }

    public String getParam2() {
        return param2;
    }

    public void setParam2(String param2) {
        this.param2 = param2;
    }

    public String getParam3() {
        return param3;
    }

    public void setParam3(String param3) {
        this.param3 = param3;
    }

}

public class Demo {

    private static BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue<Runnable>(1000000);


    private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 2000, TimeUnit.MILLISECONDS,
            bqueue, new ThreadPoolExecutor.CallerRunsPolicy());


    public static void main(String[] args)  {
        // 不需要返回用execute
        threadPoolExecutor.execute(new BusinessThread("张三", "男", "18"));
        // 需要返回用submit
        //threadPoolExecutor.submit(new BusinessThread());

    }
}
历史上的今天
December
16日
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
可以匿名评论或者登录后台评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0