最近有个业务,要查询自定义的日历,还要统计每天的数据,
for循环时间太长,想用线程来缓解下尴尬,做以下记录,免得以后忘记,大佬们勿喷
方法外启用线程
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
private final static Executor executor = Executors.newFixedThreadPool(50);// 启用多线程
String data = DateUtil.getData();
System.err.println("开始时间:"+data);
List<String> list = Collections.synchronizedList(new ArrayList());
List<Integer> a = Collections.synchronizedList(new ArrayList());
for (int i = 0 ;i<10;i++){
executor.execute(new Runnable(){
@Override
public void run(){
try {
list.add(add());
// System.err.println("i:"+i);
}catch (Exception e){
System.err.println("出错了");
}finally {
a.add(1);
System.err.println("线程ID:"+Thread.currentThread().getId());
System.err.println("线程名称:"+Thread.currentThread().getName());
}
}
});
}
while (a.size()<10){
sleep(500);
}
System.err.println("一共几个:"+list.size());
System.err.println("值是多少:"+list.toString());
System.err.println("完成时间:"+DateUtil.getData());
// System.exit(0);
以上是全部代码,因为是新手一枚,所以开始没了解线程安全性问题,做了好多实验
while (a.size()<10){
sleep(500);
}
循环是为了等待线程在list中存入值,单纯的写入线程不需要循环等待
Collections.synchronizedList(new ArrayList<>())
将ArrayList转为线程安全的
引入线程的方式 二
新建java类:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
@Configuration
public class ThreadPoolConfig {
// 核心线程池大小
@Value("${thread.pool.core-pool-size}")
int corePoolSize;
// 最大线程池大小
@Value("${thread.pool.maximum-pool-size:10}")
int maximumPoolSize;
// 线程最大空闲时间
@Value("${thread.pool.keep-alive-time:10}")
long keepAliveTime;
// 线程等待队列大小
@Value("${thread.pool.work-queue.capacity:2}")
int workQueueCapacity;
@Bean
public ThreadPoolExecutor threadPoolExecutor() {
// 时间单位
TimeUnit unit = TimeUnit.SECONDS;
// 线程等待队列
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(workQueueCapacity);
// 线程创建工厂
ThreadFactory threadFactory = new NameTreadFactory();
// 拒绝策略
RejectedExecutionHandler handler = new MyIgnorePolicy();
return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit,
workQueue, threadFactory, handler);
}
static class NameTreadFactory implements ThreadFactory {
private final AtomicInteger mThreadNum = new AtomicInteger(1);
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "my-thread-" + mThreadNum.getAndIncrement());
System.out.println(t.getName() + " has been created");
return t;
}
}
public static class MyIgnorePolicy implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
doLog(r, e);
}
private void doLog(Runnable r, ThreadPoolExecutor e) {
// 可做日志记录等
System.err.println(r.toString() + " rejected");
// System.out.println("completedTaskCount: " + e.getCompletedTaskCount());
}
}
代码中引用
@Autoword
private ThreadPoolExecutor threadPoolExecutor;
方法中写法(lambda表达式写法(jdk1.8新特性)):
threadPoolExecutor.execute(() -> {
//此处为代码逻辑
});
正常写法:
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
//代码逻辑
}});
第二种方法相较于第一种方法,不用每次在每个引用的方法中 都去初始化指定数量的线程