【多线程】CountDownLatch 等待多线程执行完

CountDownLatch countDownLatch = new CountDownLatch (2); //定义二个线程 countDownLatch .countDown(); //每个线程执行完减去 countDownLatch .await(); //等待二个线程执行完


	/**
	 * 页面展示的组合
	 * @param pythonScriptBean
	 * @param groupfunds
	 * @param ratemap
	 * @param hs300
	 */
	public StrategyResultVO getStrategyBean(Map<String,List<String>> groupfunds,Map<String,String> ratemap,List<Double> hs300,List<String> tradedatelist){
		
		Map<String,Object> groupDureData=groupDureData(groupfunds,ratemap,tradedatelist); //计算

		StrategyResultVO strategyResultVO =new StrategyResultVO();
		ExecutorService executor=Executors.newFixedThreadPool(2);
		CountDownLatch  cd = new CountDownLatch (2);
		GroupInfoRunnable groupInfoRunnable=new GroupInfoRunnable(cd,strategyRecallBean,redisService,strategyBackCalculationUtil.getTotalnetassets(),strategyResultVO,hs300,groupDureData);
		FundInfoInGroupRunnable fundInfoInGroupRunnable=new FundInfoInGroupRunnable(cd,strategyRecallBean,redisService,strategyBackCalculationUtil.getTotalnetassets(),strategyResultVO,groupDureData);
		
		executor.execute(groupInfoRunnable);
		executor.execute(fundInfoInGroupRunnable);
		
		try {
			cd.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally{
			executor.shutdown();
		}
		
		return strategyResultVO;
	}

import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import com.erichfund.cljjfof.fundanalyze.bean.StrategyRecallBean;
import com.erichfund.cljjfof.fundanalyze.bean.StrategyResultVO;
import com.erichfund.framework.cache.redis.RedisService;

/**
 * 组合         
 * @author JS-007
 *
 */
public class GroupInfoRunnable implements Runnable{
	private CountDownLatch  cd;
	private StrategyRecallBean strategyRecallBean;
	private RedisService redisService;
	private double totalnetassets;
	
	private StrategyResultVO strategyResultVO;
	private List<Double> hs300;
	private Map<String,Object> groupDureData;
	
	public GroupInfoRunnable(CountDownLatch  cd,StrategyRecallBean strategyRecallBean,RedisService redisService,double totalnetassets,StrategyResultVO strategyResultVO,List<Double> hs300,Map<String,Object> groupDureData){
		this.strategyRecallBean=strategyRecallBean;
		this.redisService=redisService;
		this.totalnetassets=totalnetassets;
		this.strategyResultVO=strategyResultVO;
		this.hs300=hs300;
		this.groupDureData=groupDureData;
		this.cd=cd;
	}
	
	@Override
	public void run() {
		try {
			StrategyBackHelper strategyBackHelper=new StrategyBackHelper(strategyRecallBean,redisService,totalnetassets);
			strategyBackHelper.groupDate(strategyResultVO,groupDureData,hs300); //组合         
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			cd.countDown();
		}
	}

}
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import com.erichfund.cljjfof.fundanalyze.bean.StrategyRecallBean;
import com.erichfund.cljjfof.fundanalyze.bean.StrategyResultVO;
import com.erichfund.framework.cache.redis.RedisService;

/**
 * 每期组合的基金       
 * @author JS-007
 *
 */
public class FundInfoInGroupRunnable implements Runnable{
	private CountDownLatch cd;
	private StrategyRecallBean strategyRecallBean;
	private RedisService redisService;
	private double totalnetassets;
	
	private StrategyResultVO strategyResultVO;
	private Map<String,Object> groupDureData;
	
	public FundInfoInGroupRunnable(CountDownLatch cd,StrategyRecallBean strategyRecallBean,RedisService redisService,double totalnetassets,StrategyResultVO strategyResultVO,Map<String,Object> groupDureData){
		this.strategyRecallBean=strategyRecallBean;
		this.redisService=redisService;
		this.totalnetassets=totalnetassets;
		this.strategyResultVO=strategyResultVO;
		this.groupDureData=groupDureData;
		this.cd=cd;
	}
	
	@Override
	public void run() {
		try {
			StrategyBackHelper strategyBackHelper=new StrategyBackHelper(strategyRecallBean,redisService,totalnetassets);
			strategyBackHelper.getStrategyInfoBeanList(strategyResultVO,groupDureData);//每期组合的基金          
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			cd.countDown();
		}
	}

}

CyclicBarrier


import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {
	public static void main(String[] args) {
		CyclicBarrierDemo CyclicBarrierDemo=new CyclicBarrierDemo();
		System.out.println(CyclicBarrierDemo.ddd());
	}
	
	public String ddd(){
		final CyclicBarrier  cd = new CyclicBarrier(3,new Runnable() {
			@Override
			public void run() {
				System.out.println("end");
			}
		});
		new Thread(new Runnable(){
			public void run(){
				try {
					System.out.println("111111111111111111111");
					cd.await();
				} catch (Exception e) {
					e.printStackTrace();
				}             
			}
		}).start();
		new Thread(new Runnable(){
			public void run(){
				try {
					System.out.println("222222222222222222222");
					cd.await();
				} catch (Exception e) {
					e.printStackTrace();
				}               
			}
		}).start();
		
		//当前线程等待
		try {
			cd.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (BrokenBarrierException e) {
			e.printStackTrace();
		}
		
		return "44444";
	}
}

转载于:https://my.oschina.net/v512345/blog/1421853

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值