java Concurrent 并发包的使用

雨燕双飞 提交于 2019-12-05 10:31:25

最近在公司里面一直有开发银行对账的需求,但是一直由于项目排期比较的密集,所以该功能已知在推脱。

利用闲余的时间,使用并发包的机制,实现部分的逻辑代码!

先看下对账的业务对象。


package com.concurrent.ule;

import java.io.Serializable;

public class Reconciliation implements Serializable {
	private static final long serialVersionUID = 1L;
		private int payType;//支付类型
		private int paySubType;//支付子类型
		private float payedAmount;//支付总账 
		private float refundAmount;//退款总账
		
		private float bankAcceptedAmount; // 银行收账
		private float bankRefundAmount;//银行退款
		public int getPayType() {
			return payType;
		}
		public void setPayType(int payType) {
			this.payType = payType;
		}
		public int getPaySubType() {
			return paySubType;
		}
		public void setPaySubType(int paySubType) {
			this.paySubType = paySubType;
		}
		public float getPayedAmount() {
			return payedAmount;
		}
		public void setPayedAmount(float payedAmount) {
			this.payedAmount = payedAmount;
		}
		public float getRefundAmount() {
			return refundAmount;
		}
		public void setRefundAmount(float refundAmount) {
			this.refundAmount = refundAmount;
		}
		public float getBankAcceptedAmount() {
			return bankAcceptedAmount;
		}
		public void setBankAcceptedAmount(float bankAcceptedAmount) {
			this.bankAcceptedAmount = bankAcceptedAmount;
		}
		public float getBankRefundAmount() {
			return bankRefundAmount;
		}
		public void setBankRefundAmount(float bankRefundAmount) {
			this.bankRefundAmount = bankRefundAmount;
		}
		
		public String toString(){
			return "Reconciliation data: [ payType:"+payType 
					+",paySubType:"+paySubType
					+",payedAmount:"+payedAmount
					+",refundAmount:"+refundAmount
					+",bankAcceptedAmount:"+bankAcceptedAmount
					+",bankRefundAmount:"+bankRefundAmount+" ]";
		}
}
模拟产生不同的银行支付类型中对账数据。


  1.  使用借记卡的形式支付对对账的数据。
    package com.concurrent.ule.task;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.Callable;
    import java.util.concurrent.ConcurrentHashMap;
    
    import com.concurrent.ule.Reconciliation;
    
    
    public class UleCardReconciliationTask implements Callable<Map<String,Reconciliation>> {
    	private ConcurrentHashMap<String,Reconciliation> resultMap;
    	
    	public UleCardReconciliationTask(
    			ConcurrentHashMap<String, Reconciliation> resultMap) {
    		this.resultMap = resultMap;
    	}
    
    	@Override
    	public Map<String,Reconciliation> call() throws Exception {
    		System.out.println("begin to enter Ule11 data generator");
    		//Map<String,Reconciliation>  resultMap=new HashMap<String,Reconciliation>();
    		List<Reconciliation> recs=getDataFromParseXmlFile();
    		for(Reconciliation info:recs){
    			resultMap.put(""+info.getPayType()+"_"+info.getPaySubType(), info);
    		}
    		return resultMap;
    	}
    	
    	private List<Reconciliation> getDataFromParseXmlFile() throws InterruptedException{
    		Thread.sleep(1000);
    		List<Reconciliation> recs=new ArrayList<Reconciliation>();
    		Reconciliation rec0=new Reconciliation();
    		rec0.setPayType(9);
    		rec0.setPaySubType(10);
    		rec0.setBankAcceptedAmount(200.00f);
    		rec0.setBankRefundAmount(20.00f);
    		recs.add(rec0);
    		return recs;
    	}
    
    }
    1. 使用信用卡的支付方式;
      package com.concurrent.ule.task;
      
      import java.util.ArrayList;
      import java.util.HashMap;
      import java.util.List;
      import java.util.Map;
      import java.util.concurrent.Callable;
      import java.util.concurrent.ConcurrentHashMap;
      
      import com.concurrent.ule.Reconciliation;
      
      
      public class UleCreditReconciliationTask implements Callable<Map<String,Reconciliation>> {
      	private ConcurrentHashMap<String,Reconciliation> resultMap;
      	
      	public UleCreditReconciliationTask(ConcurrentHashMap<String,Reconciliation> resultMap) {
      		this.resultMap = resultMap;
      	}
      
      	@Override
      	public Map<String,Reconciliation> call() throws Exception {
      		System.out.println("begin to enter Ule22 data generator");
      		//Map<String,Reconciliation>  resultMap=new HashMap<String,Reconciliation>();
      		List<Reconciliation> recs=getDataFromParseExcelFile();
      		for(Reconciliation info:recs){
      			resultMap.put(""+info.getPayType()+"_"+info.getPaySubType(), info);
      		}
      		return resultMap;
      	}
      	
      	private List<Reconciliation> getDataFromParseExcelFile() throws InterruptedException{
      		Thread.sleep(1000);
      		List<Reconciliation> recs=new ArrayList<Reconciliation>();
      		Reconciliation rec0=new Reconciliation();
      		rec0.setPayType(9);
      		rec0.setPaySubType(9);
      		rec0.setBankAcceptedAmount(200.00f);
      		rec0.setBankRefundAmount(20.00f);
      		recs.add(rec0);
      		return recs;
      	}
      
      }
    2. 使用快捷卡的支付方式。
      package com.concurrent.ule.task;
      
      import java.util.ArrayList;
      import java.util.HashMap;
      import java.util.List;
      import java.util.Map;
      import java.util.concurrent.Callable;
      import java.util.concurrent.ConcurrentHashMap;
      
      import com.concurrent.ule.Reconciliation;
      
      
      public class UleQuickReconciliationTask implements Callable<Map<String,Reconciliation>> {
      	private ConcurrentHashMap<String,Reconciliation> resultMap;
      	
      	public UleQuickReconciliationTask(
      			ConcurrentHashMap<String, Reconciliation> resultMap) {
      		this.resultMap = resultMap;
      	}
      
      	@Override
      	public Map<String,Reconciliation> call() throws Exception {
      		System.out.println("begin to enter Ule33 data generator");
      		//Map<String,Reconciliation>  resultMap=new HashMap<String,Reconciliation>();
      		List<Reconciliation> recs=getDataFromEJBApi();
      		for(Reconciliation info:recs){
      			resultMap.put(""+info.getPayType()+"_"+info.getPaySubType(), info);
      		}
      		return resultMap;
      	}
      	
      	private List<Reconciliation> getDataFromEJBApi() throws InterruptedException{
      		Thread.sleep(1000);
      		List<Reconciliation> recs=new ArrayList<Reconciliation>();
      		Reconciliation rec0=new Reconciliation();
      		rec0.setPayType(9);
      		rec0.setPaySubType(11);
      		rec0.setBankAcceptedAmount(200.00f);
      		rec0.setBankRefundAmount(20.00f);
      		recs.add(rec0);
      		return recs;
      	}
      
      }
    3. 自身系统的支付数据样式:
      package com.concurrent.ule.task;
      
      import java.util.ArrayList;
      import java.util.HashMap;
      import java.util.List;
      import java.util.Map;
      import java.util.concurrent.Callable;
      import java.util.concurrent.ConcurrentHashMap;
      
      import com.concurrent.ule.Reconciliation;
      
      
      public class UleReconciliationTask implements Callable<Map<String,Reconciliation>> {
      	private ConcurrentHashMap<String,Reconciliation> resultMap;
      	
      	
      	public UleReconciliationTask(
      			ConcurrentHashMap<String, Reconciliation> resultMap) {
      		this.resultMap = resultMap;
      	}
      
      	@Override
      	public Map<String,Reconciliation> call() throws Exception {
      		System.out.println("begin to enter Ule00  data generator");
      		//Map<String,Reconciliation>  resultMap=new HashMap<String,Reconciliation>();
      		List<Reconciliation> recs=getDataFromDB();
      		for(Reconciliation info:recs){
      			resultMap.put(""+info.getPayType()+"_"+info.getPaySubType(), info);
      		}
      		return resultMap;
      	}
      	
      	private List<Reconciliation> getDataFromDB() throws InterruptedException{
      		Thread.sleep(1000);
      		List<Reconciliation> recs=new ArrayList<Reconciliation>();
      		Reconciliation rec0=new Reconciliation();
      		rec0.setPayType(9);
      		rec0.setPaySubType(9);
      		rec0.setPayedAmount(300.00f);
      		rec0.setRefundAmount(30.00f);
      		
      		
      		Reconciliation rec1=new Reconciliation();
      		rec1.setPayType(9);
      		rec1.setPaySubType(10);
      		rec1.setPayedAmount(300.00f);
      		rec1.setRefundAmount(30.00f);
      		recs.add(rec0);
      		recs.add(rec1);
      		
      		Reconciliation rec2=new Reconciliation();
      		rec2.setPayType(9);
      		rec2.setPaySubType(11);
      		rec2.setPayedAmount(300.00f);
      		rec2.setRefundAmount(30.00f);
      		recs.add(rec2);
      		
      
      		return recs;
      	}
      
      }
    使用一个固定线程池来启动各种任务:
    package com.concurrent.ule.task;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.concurrent.Callable;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    
    import com.concurrent.ule.Reconciliation;
    
    
    public class ReConciliationEngineer {
    	public static void main(String[] args) throws Exception {
    		ReConciliationEngineer re=new ReConciliationEngineer();
    		re.excute();
    	}
    
    	public void excute() throws Exception{
    		Long begin=System.currentTimeMillis();
    		ExecutorService pool = Executors.newFixedThreadPool(4);
            Set<Future<Map<String,Reconciliation>>> set = new HashSet<Future<Map<String,Reconciliation> >>();
                //Callable<Map<String,Reconciliation> > callable = new UleCardReconciliationTask();
            ConcurrentHashMap<String,Reconciliation>  resultMap=new ConcurrentHashMap<String,Reconciliation>();
                List<Callable> callables=buildTask(resultMap);
                for(Callable callable:callables){
    	            Future<Map<String,Reconciliation> > future1 = pool.submit(callable);
    	            set.add(future1);
                }
                Long end=System.currentTimeMillis();
                System.out.println("Multiple Thread Total cost:"+(end-begin));
            for (Future<Map<String,Reconciliation>> future : set) {
            	Map<String,Reconciliation> 	a=future.get();
            	System.out.println(a.toString());
            }
            pool.shutdown();
            Long begin1=System.currentTimeMillis();
            ConcurrentHashMap<String,Reconciliation>  resultMap2=new ConcurrentHashMap<String,Reconciliation>();
            UleReconciliationTask t0=new UleReconciliationTask(resultMap2);
            t0.call();
            UleCardReconciliationTask t1=new UleCardReconciliationTask(resultMap2);
            t1.call();
            UleCreditReconciliationTask t2=new UleCreditReconciliationTask(resultMap2);
            t2.call();
            UleQuickReconciliationTask t3=new UleQuickReconciliationTask(resultMap2);
            t3.call();
            Long end1=System.currentTimeMillis();
            System.out.println("Single Thread Total cost:"+(end1-begin1));
            
    	}
    	public List<Callable> buildTask(ConcurrentHashMap<String,Reconciliation>  resultMap){
    		List<Callable> list=new ArrayList<Callable>();
    		list.add(new UleReconciliationTask(resultMap));
    		list.add(new UleCardReconciliationTask(resultMap));
    		list.add(new UleCreditReconciliationTask(resultMap));
    		list.add(new UleQuickReconciliationTask(resultMap));
    		
    		return list;
    	}
    }
  2. 性能对比:
    begin to enter Ule00  data generator
    Multiple Thread Total cost:62
    begin to enter Ule22 data generator
    begin to enter Ule11 data generator
    begin to enter Ule33 data generator
    {9_10=Reconciliation data: [ payType:9,paySubType:10,payedAmount:300.0,refundAmount:30.0,bankAcceptedAmount:0.0,bankRefundAmount:0.0 ], 9_9=Reconciliation data: [ payType:9,paySubType:9,payedAmount:300.0,refundAmount:30.0,bankAcceptedAmount:0.0,bankRefundAmount:0.0 ], 9_11=Reconciliation data: [ payType:9,paySubType:11,payedAmount:0.0,refundAmount:0.0,bankAcceptedAmount:200.0,bankRefundAmount:20.0 ]}
    {9_10=Reconciliation data: [ payType:9,paySubType:10,payedAmount:300.0,refundAmount:30.0,bankAcceptedAmount:0.0,bankRefundAmount:0.0 ], 9_9=Reconciliation data: [ payType:9,paySubType:9,payedAmount:300.0,refundAmount:30.0,bankAcceptedAmount:0.0,bankRefundAmount:0.0 ], 9_11=Reconciliation data: [ payType:9,paySubType:11,payedAmount:0.0,refundAmount:0.0,bankAcceptedAmount:200.0,bankRefundAmount:20.0 ]}
    {9_10=Reconciliation data: [ payType:9,paySubType:10,payedAmount:300.0,refundAmount:30.0,bankAcceptedAmount:0.0,bankRefundAmount:0.0 ], 9_9=Reconciliation data: [ payType:9,paySubType:9,payedAmount:300.0,refundAmount:30.0,bankAcceptedAmount:0.0,bankRefundAmount:0.0 ], 9_11=Reconciliation data: [ payType:9,paySubType:11,payedAmount:0.0,refundAmount:0.0,bankAcceptedAmount:200.0,bankRefundAmount:20.0 ]}
    {9_10=Reconciliation data: [ payType:9,paySubType:10,payedAmount:300.0,refundAmount:30.0,bankAcceptedAmount:0.0,bankRefundAmount:0.0 ], 9_9=Reconciliation data: [ payType:9,paySubType:9,payedAmount:300.0,refundAmount:30.0,bankAcceptedAmount:0.0,bankRefundAmount:0.0 ], 9_11=Reconciliation data: [ payType:9,paySubType:11,payedAmount:0.0,refundAmount:0.0,bankAcceptedAmount:200.0,bankRefundAmount:20.0 ]}
    begin to enter Ule00  data generator
    begin to enter Ule11 data generator
    begin to enter Ule22 data generator
    begin to enter Ule33 data generator
    Single Thread Total cost:4000
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!