Executors几种创建方式

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

线程池数如何设置

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

多线程之Executors基本使用的更多相关文章

  1. java 线程之executors线程池

    一.线程池的作用 平时的业务中,如果要使用多线程,那么我们会在业务开始前创建线程,业务结束后,销毁线程.但是对于业务来说,线程的创建和销毁是与业务本身无关的,只关心线程所执行的任务.因此希望把尽可能多 ...

  2. Java基础-进程与线程之Thread类详解

    Java基础-进程与线程之Thread类详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.进程与线程的区别 简而言之:一个程序运行后至少有一个进程,一个进程中可以包含多个线程 ...

  3. JAVA多线程之UncaughtExceptionHandler——处理非正常的线程中止

    JAVA多线程之UncaughtExceptionHandler——处理非正常的线程中止 背景 当单线程的程序发生一个未捕获的异常时我们可以采用try....catch进行异常的捕获,但是在多线程环境 ...

  4. iOS多线程之8.NSOPeration的其他用法

      本文主要对NSOPeration的一些重点属性和方法做出介绍,以便大家可以更好的使用NSOPeration. 1.添加依赖 - (void)addDependency:(NSOperation * ...

  5. python 线程之 threading(四)

    python 线程之 threading(三) http://www.cnblogs.com/someoneHan/p/6213100.html中对Event做了简单的介绍. 但是如果线程打算一遍一遍 ...

  6. python 线程之 threading(三)

    python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...

  7. python 线程之_thread

    python 线程之_thread _thread module: 基本用法: def child(tid): print("hello from child",tid) _thr ...

  8. Java多线程之ConcurrentSkipListMap深入分析(转)

    Java多线程之ConcurrentSkipListMap深入分析   一.前言 concurrentHashMap与ConcurrentSkipListMap性能测试 在4线程1.6万数据的条件下, ...

  9. 【C#】线程之Parallel

    在一些常见的编程情形中,使用任务也许能提升性能.为了简化变成,静态类System.Threading.Tasks.Parallel封装了这些常见的情形,它内部使用Task对象. Parallel.Fo ...

随机推荐

  1. MS SQL Server NULL处理

    -- 首先在用户表中插入数据如下 TRUNCATE TABLE UserInfo ; INSERT INTO userinfo(UserName,UserLogin,UserPassword,User ...

  2. RabbitMQ基础知识及Linux安装

    RabbitMQ: RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现. AMQP协议: AMQP,即Advanced Message Qu ...

  3. 网络流24题 ——运输问题 luogu 4015

    题目描述:这里 题面已经提示我们这是费用流了 那么由源点向所有仓库连边,容量为仓库原有货物量,费用为0 然后由所有零售商店向汇点连边,容量为一个零售商店的需求量,费用为0 最后由仓库向零售商店连边,容 ...

  4. Eclipse安装git插件以及关联导入GitHub项目

    一.Eclipse配置git 1.查看自己eclipse的版本 打开eclipse 导航: help->AboutEclipse 如图: 2.检查Eclipse中是否已安装Git插件 菜单栏He ...

  5. 饮冰三年-人工智能-Python-24 Django ORM增删改查

    一:首先使用默认的sqlite3创建表 1:现在在models.py中添加表模型 from django.db import models # Create your models here. cla ...

  6. jquery刷新页面代码

    window.location.reload()刷新当前页面.parent.location.reload()刷新父亲对象(用于框架)opener.location.reload()刷新父窗口对象(用 ...

  7. CSS之三个模型 盒子模型 轮廓模型 内外边距

    盒子模型 最终元素的总宽度计算公式是这样的: 总元素的宽度=宽度+左填充+右填充+左边框+右边框+左边距+右边距 元素的总高度最终计算公式是这样的: 总元素的高度=高度+顶部填充+底部填充+上边框+下 ...

  8. 金蝶K/3 同步用核算项目配置

  9. Java Spring Boot VS .NetCore (七) 配置文件

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  10. php正则验证车牌格式的函数

    /** * 判断是否合法车牌号 * @name isCarLicense * @param $license * @return bool */ function isCarLicense($lice ...