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. 【easy】88. Merge Sorted Array 合并两个有序数组

    合并两个有序的list 把排序好的nums2插入nums1中,假设nums1这个vector的空间永远是够的 思路:倒序!! class Solution { public: void merge(v ...

  2. Python-Django 模型层-单表查询

    单表操作 -增加,删,改:两种方式:queryset对象的方法,book对象的方法 -改:需要用save() -get()方法:查询的数据有且只有一条,如果多,少,都抛异常 单表查询 -<1&g ...

  3. linux SWAP

    1.内存和SWAP之间合理的分配方案 M = Amount of RAM in GB, and S = Amount of swap in GB, then If M < 2, S = M *2 ...

  4. linux基础一篇就够了

    Linux学习笔记 粗略笔记第一版,全文约2000行50000字 1. 时间和日历 date:查看当前时间 cal:查看当月日历 cal 2018:查看年日历 cal 10 2018:指定某年某月日历 ...

  5. Emacs Org-mode 4 超连接

    4.1 连接格式 连接的格式非常的简单,示例如下: [[文档内部锚点.外部连接][对连接的描述,可选]] 4.2 内部连接 想要引用或者连接到文档自身内的某个位置,需要引入另外一个概念:anchor( ...

  6. 解码escape类型的unicode

    content = Regex.Unescape(content);

  7. SSL双向认证和SSL单向认证的流程和区别

    refs: SSL双向认证和SSL单向认证的区别https://www.jianshu.com/p/fb5fe0165ef2 图解 https 单向认证和双向认证!https://cloud.tenc ...

  8. scrapy相关:splash 实践

    0. 1.参考 https://github.com/scrapy-plugins/scrapy-splash#configuration 以此为准 scrapy相关:splash安装 A javas ...

  9. 解决:coursera 视频总是缓冲或者无法观看

    关于这个问题,网上有很多的答案,但是可能我是win10 最近才更新了的,网上的方法都不能完全解决,然后自己搜了哈,最后综合自己解决了.具体方法如下. 在开始菜单中打开运行命令,输入gpedit.msc ...

  10. XVIII Open Cup named after E.V. Pankratiev. Ukrainian Grand Prix

    A. Accommodation Plan 对于已知的$K$个点,离它们距离都不超过$L$的点在树上是一个连通块,考虑在每种方案对应的离$1$最近的点统计. 即对于每个点$x$,统计离它距离不超过$L ...