多线程之Executors基本使用
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基本使用的更多相关文章
- java 线程之executors线程池
一.线程池的作用 平时的业务中,如果要使用多线程,那么我们会在业务开始前创建线程,业务结束后,销毁线程.但是对于业务来说,线程的创建和销毁是与业务本身无关的,只关心线程所执行的任务.因此希望把尽可能多 ...
- Java基础-进程与线程之Thread类详解
Java基础-进程与线程之Thread类详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.进程与线程的区别 简而言之:一个程序运行后至少有一个进程,一个进程中可以包含多个线程 ...
- JAVA多线程之UncaughtExceptionHandler——处理非正常的线程中止
JAVA多线程之UncaughtExceptionHandler——处理非正常的线程中止 背景 当单线程的程序发生一个未捕获的异常时我们可以采用try....catch进行异常的捕获,但是在多线程环境 ...
- iOS多线程之8.NSOPeration的其他用法
本文主要对NSOPeration的一些重点属性和方法做出介绍,以便大家可以更好的使用NSOPeration. 1.添加依赖 - (void)addDependency:(NSOperation * ...
- python 线程之 threading(四)
python 线程之 threading(三) http://www.cnblogs.com/someoneHan/p/6213100.html中对Event做了简单的介绍. 但是如果线程打算一遍一遍 ...
- python 线程之 threading(三)
python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...
- python 线程之_thread
python 线程之_thread _thread module: 基本用法: def child(tid): print("hello from child",tid) _thr ...
- Java多线程之ConcurrentSkipListMap深入分析(转)
Java多线程之ConcurrentSkipListMap深入分析 一.前言 concurrentHashMap与ConcurrentSkipListMap性能测试 在4线程1.6万数据的条件下, ...
- 【C#】线程之Parallel
在一些常见的编程情形中,使用任务也许能提升性能.为了简化变成,静态类System.Threading.Tasks.Parallel封装了这些常见的情形,它内部使用Task对象. Parallel.Fo ...
随机推荐
- [转载]解决在win10中webstrom无法使用命令行(Terminal)
转载地址:https://qiaolevip.iteye.com/blog/2217688 原因:计算机从win7更新到win10,webstorm9命令框无法输入,以为是webstorm问题和win ...
- [CF662C] Binary Table(FWT)
题意: https://www.cnblogs.com/cjyyb/p/9065801.html 题解:
- Metaphor of topological basis and open set
The definition of topological basis for a space $X$ requires that each point $x$ in $X$ is contained ...
- vue-cli 构建的项目中 如何使用less
vue-cli 构建的项目默认是不支持 less 的,需要自己添加. 首选,安装 less 和 less-loader ,在项目目录下运行如下命令 npm install less less-load ...
- 第十一篇 CBV和闪现
前几篇写的都是FBV 现在可以了解一下CBV CBV 其实就是把请求方式都写到一个类中 学过django的一眼应该就明白了 from flask import Flask, render_templa ...
- 如何运行后台Service?
Unless you specify otherwise, most of the operations you do in an app run in the foreground on a spe ...
- cocos 游戏开发 (第一天作业)
作业1——控制台游戏菜单 // 游戏菜单.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> ...
- You must configure either the server or JDBC driver (via the serverTimezone configuration property
使用JDBC连接MySql时出现:The server time zone value '�й���ʱ��' is unrecognized or represents more than one ...
- Object类的方法
一 Object:类Object是类层次结构的跟类,每个类都使用Object作为超类,每个类都是直接或者间接的继承自Object类. Object类的方法: ①public int hashCode( ...
- 找出数组[1...n]中第k小元素
//问题描述: 试编写一个算法,使之能够在数组L[1...n]中找出第k小的元素(即从小到大排序后处于第k个位置的元素) #include <stdio.h> // 结合快排思想,查找第5 ...