Executors几种创建方式

https://www.cnblogs.com/yasong/p/9318522.html

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

线程池数如何设置

https://blog.csdn.net/u013276277/article/details/82630336

https://www.cnblogs.com/cherish010/p/8334952.html

示例

  • 实现Runnable方式
public class WorkThread implements Runnable{
    private String mobile = "";
    private Integer num;
    
    @Override
    public void run() {
        String reqData = mobile;
        try {
            System.out.println(Thread.currentThread().getName()+"线程"+"["+num+"]request data :"+reqData);
            String respStr = accessScoreService(reqData);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * 具体业务处理逻辑
     * 
     * @param reqData
     * @return
     */
    private String accessScoreService(String reqData) {
        return Thread.currentThread().getName()+"线程执行任务";
    }
    
    public WorkThread(String _mobile,Integer num){
        this.mobile = _mobile;
        this.num=num;
    }
    public WorkThread(){
    }
}

测试:

/**
     * 测试多线程
     */
    @Test
    public void testExecutors(){
            List<String> mobList=new ArrayList<>();
            for(int i=10;i<100;i++){
                String tel="186544444"+i;
                mobList.add(tel);
            }
            Integer tps=10;
            ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
            int sleepTime = 1000 / tps;

            for(Integer i=0,size=mobList.size();i<size;i++){
                String mob=mobList.get(i);
                fixedThreadPool.execute(new WorkThread(mob, i));
                /*try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
            }
            System.out.println("线程初始化完成");
    }
  • 实现Callable方式
public class WorkForCallable implements Callable<String> {

    private String mobile;
    
    @Override
    public String call() throws Exception {
        System.out.println("************开始执行"+"["+Thread.currentThread().getName()+"]"+"线程=======处理任务:"+mobile);
        return "["+Thread.currentThread().getName()+"]"+"线程处理成功";
    }
    
    public WorkForCallable(String mobile){
        this.mobile=mobile;
    }
    public WorkForCallable(){
    }
}

测试:

@Test
    public void testExecutors1() throws InterruptedException, ExecutionException {
        List<String> mobList=new ArrayList<>();
        for(int i=10;i<100;i++){
            String tel="186544444"+i;
            mobList.add(tel);
        }
        List<FutureTask<String>> futureTaskList = new ArrayList<FutureTask<String>>();
        ExecutorService excutorService = Executors.newFixedThreadPool(10);
        for(int a=0;a<mobList.size();a++){
            String str=mobList.get(a);
            FutureTask<String> futureTask = new FutureTask<String>(new WorkForCallable(str));
           // futureTaskList.add(futureTask);
            excutorService.submit(futureTask);
            String s = futureTask.get();
            System.out.println("************完成结果"+s+"手机号:"+str);
        }
    }

 

学习链接

https://blog.csdn.net/m0_37825799/article/details/79088596

https://www.cnblogs.com/zengyuanjun/p/8094610.html

https://blog.csdn.net/majunzhu/article/details/83013780

https://blog.csdn.net/qq_32725403/article/details/79488068

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄