文档章节

java Concurrent 并发包的使用

保罗的寓言
 保罗的寓言
发布于 2012/11/08 01:12
字数 1239
阅读 2854
收藏 50

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

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

先看下对账的业务对象。


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

© 著作权归作者所有

下一篇: drools
保罗的寓言
粉丝 25
博文 70
码字总数 40506
作品 0
杨浦
高级程序员
私信 提问
加载中

评论(2)

ABCA的A
ABCA的A
感谢分享
小痴
小痴
学习了!!!!
读书笔记之《Java并发编程的艺术》-并发编程容器和框架(重要)

读书笔记部分内容来源书出版书,版权归本书作者,如有错误,请指正。 欢迎star、fork,读书笔记系列会同步更新 git https://github.com/xuminwlt/j360-jdk module j360-jdk-thread/me.j360....

Hi徐敏
2015/11/11
722
1
Java里的nio和concurrent是在什么时候使用的?是在高性能web应用中使用吗?

Java里的nio和concurrent是在什么应用中使用的? 是在高并发的网站中使用的吗? 我发现只有少数的工作强调core Java的multi-thread,比如concurrent,这是为什么?...

文心雕码
2013/12/20
973
4
Java 7 的新特性一览表

官方说是 7月28日 正式发布 Java 7 ,正常的话我们应该在 7月29日 看到这个版本。很快了,就两天时间。 发布之前让我们先来看看 Java 7 都有什么新特性吧。 Java 7 的架构图: 新特性一览表:...

红薯
2011/07/27
77.5K
83
System.gc() 源码解读

System.gc() 源码解读 简书 涤生。 转载请注明原创出处,谢谢! 如果读完觉得有收获的话,欢迎点赞加关注。 介绍 System.gc(),大家应该也有所了解,是JDK提供的触发Full GC的一种方式,会触...

涤生_YinQi
2018/01/14
0
0
Java系列笔记 - Java 内存区域和GC机制

Java垃圾回收概况   Java GC(Garbage Collection,垃圾收集,垃圾回收)机制,是Java与C++/C的主要区别之一,作为Java开发者,一般不需要专门编写内存回收和垃圾清理代码,对内存泄露和溢...

明舞
2015/08/18
2.1K
6

没有更多内容

加载失败,请刷新页面

加载更多

nginx学习笔记

中间件位于客户机/ 服务器的操作系统之上,管理计算机资源和网络通讯。 是连接两个独立应用程序或独立系统的软件。 web请求通过中间件可以直接调用操作系统,也可以经过中间件把请求分发到多...

码农实战
今天
5
0
Spring Security 实战干货:玩转自定义登录

1. 前言 前面的关于 Spring Security 相关的文章只是一个预热。为了接下来更好的实战,如果你错过了请从 Spring Security 实战系列 开始。安全访问的第一步就是认证(Authentication),认证...

码农小胖哥
今天
10
0
JAVA 实现雪花算法生成唯一订单号工具类

import lombok.SneakyThrows;import lombok.extern.slf4j.Slf4j;import java.util.Calendar;/** * Default distributed primary key generator. * * <p> * Use snowflake......

huangkejie
昨天
12
0
PhotoShop 色调:RGB/CMYK 颜色模式

一·、 RGB : 三原色:红绿蓝 1.通道:通道中的红绿蓝通道分别对应的是红绿蓝三种原色(RGB)的显示范围 1.差值模式能模拟三种原色叠加之后的效果 2.添加-颜色曲线:调整图像RGB颜色----R色增强...

东方墨天
昨天
11
1
将博客搬至CSDN

将博客搬至CSDN

算法与编程之美
昨天
13
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部