等待线程池中线程执行完毕

利用原子计数器技术,等于任务数时返回 

package com.qyc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qyc.entity.HaxxData;
import com.qyc.service.ShowBigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.LongAdder;

@Service
public class ShowBigServiceImpl implements ShowBigService {

    @Autowired
    private StudentServiceImpl stuService;

    @Autowired
    private StuInfoServiceImpl stuInfoService;

    @Override
//    @Cacheable(cacheNames = "HaxxData")
    public HaxxData updateData() {
        HaxxData haxxData = new HaxxData();
        //获取总人数
        haxxData.setSum(stuService.selectSum());

        //获取就业人数
        haxxData.setNow(stuService.selectNow());

        //获取就业率
        double emp = Double.valueOf(haxxData.getNow())/Double.valueOf(haxxData.getSum());
        emp*=100;
        BigDecimal b   =   new   BigDecimal(emp);
        haxxData.setEmploymentRate(b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue());
        ExecutorService pool = Executors.newFixedThreadPool(5);
        //原子计数器
        LongAdder adder = new LongAdder();
        //获取就业类型
        pool.execute(()->{
            haxxData.setIndustry(stuInfoService.getIndustry());
            adder.increment();
        });

        //获取专业
        pool.execute(()->{
            haxxData.setMajor(stuInfoService.getmajor());
            adder.increment();
        });

        //获取日期
        pool.execute(()->{
            haxxData.setTimeY(stuInfoService.getTime().get("y"));
            haxxData.setTimeX(stuInfoService.getTime().get("x"));
            adder.increment();
        });

        //获取工资
        pool.execute(()->{
            haxxData.setPay(stuInfoService.getPay());
            adder.increment();
        });

        //获取福利
        pool.execute(()->{
            haxxData.setWelfare(stuInfoService.getWelfare());
            adder.increment();
        });

        //获取渠道
        pool.execute(()->{
            haxxData.setChannel(stuInfoService.getChannel());
            adder.increment();
        });
        //判断
        while (true){
            if(adder.sum()==6l){
                return haxxData;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41835813/article/details/114157436