多线程迭代for循环查询

最近有个业务,要查询自定义的日历,还要统计每天的数据,
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() {
                                    //代码逻辑
                                    }});

第二种方法相较于第一种方法,不用每次在每个引用的方法中 都去初始化指定数量的线程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小可乐-我一直在

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值