最近在公司裏面一直有開發銀行對帳的需求,可是一直因爲項目排期比較的密集,因此該功能已知在推脫。 java
利用閒餘的時間,使用併發包的機制,實現部分的邏輯代碼! 併發
先看下對帳的業務對象。 ide
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+" ]"; } }模擬產生不一樣的銀行支付類型中對帳數據。
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; } }
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; } }
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; } }
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; } }
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