线程池的使用简单示例

avatar 2020年12月16日18:31:08 5 2252 views
博主分享免费Java教学视频,B站账号:Java刘哥 ,长期提供技术问题解决、项目定制:本站商品点此

直接上代码

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());

    }
}
  • 微信
  • 交流学习,资料分享
  • weinxin
  • 个人淘宝
  • 店铺名:言曌博客咨询部

  • (部分商品未及时上架淘宝)
avatar

发表评论

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

  

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