多线程之ConcurrentLinkedQueue

ConcurrentLinkedQueue用法

package com.cn.test1;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 此需求的特点:先进进出 ,
 * 多线程处理  ConcurrentLinkedQueue应用
 * pop()  
 * 考虑下列需求:典型的生产者消费者模块 先进先出原则
 */
public class Test {
	
	
	public static void main(String[] args) {
		Test t  = new Test() ;
		t.inportExcel(20000) ;
		
	}
	
	/**
	 * 模拟生成需要导入的数据
	 * @param size
	 * @return
	 */
	public List<String> initData(int size){
		List<String> data = new ArrayList<String>() ;
		for(int i = 1 ; i<size ; i++){
			data.add("Object-"+i) ;
		}
		return data ;
	}
	
	public void inportExcel(int size){
		long t1 = System.currentTimeMillis() ;
		try {
			List<String> data = initData(size) ;
			Queue<String> queue = new ConcurrentLinkedQueue<String>(data);
			int poolSize = queue.size()<20?1:200;
			ExecutorService pool = Executors.newFixedThreadPool(poolSize);
			
			
			BatchAddWeakPasswordResult result = new BatchAddWeakPasswordResult();
			Count ctn = new Count(data.size());
			Object o = new Object();
			
			for(int i=0;i<poolSize;i++){
				Thread t = new AddPWDThread(queue,result,o, ctn);
				pool.execute(t);
			}
			synchronized(o){
				if((!pool.isTerminated() && ctn.i!=0) || queue.isEmpty() ){
					o.wait(1000*60);
				}
			}
			//任务处理完成关掉线程池
			pool.shutdown() ;
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long t2 = System.currentTimeMillis() ;
		System.out.println("导入花费时间:(毫秒)"+ (t2 - t1));
	}
	/**
	 * 计数器
	 * @author Administrator
	 *
	 */
	class Count{
		int i = 0;
		public Count(int i){
			this.i = i;
		}
		
		public void add(){
			i++;
		}
		
		public void sub(){
			i--;
		}
		
		public int size(){
			return i;
		}
	}
	
	/**
	 * 导入线程
	 * @author Administrator
	 *
	 */
class AddPWDThread extends Thread {
		
		private Queue<String> queue;
		private BatchAddWeakPasswordResult result;
		private Object o;
		private Count size;
		
		public AddPWDThread(Queue<String> queue,BatchAddWeakPasswordResult result,Object o,Count size){
			this.queue = queue;
			this.result = result;
			this.o = o;
			this.size = size;
		}
	    @Override
	    public void run() {
	    	try {
				Thread.sleep(500) ;
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
	    	//System.out.println("###############"+Thread.currentThread().getName() + "正在执行。。。");
	    	//while(queue.size() > 0){//不要用size()方法 ,会造成遍历队列 非常耗时
	    	while(!queue.isEmpty()){
    			String webSysWeakpassword = queue.poll();
    			if(webSysWeakpassword!=null){
			    	try{
			    		long t1 = System.currentTimeMillis();
					//	System.out.println(Thread.currentThread().getName() + "插入对象 "+webSysWeakpassword+" 到数据库 ");
						result.addResultMap(webSysWeakpassword, "SUCCESS",true);
					//	System.out.println("############## add pwd use time " +(System.currentTimeMillis()-t1) + "ms");
					}catch(Exception e){
						result.addResultMap(webSysWeakpassword, "ERROR",false);
					}finally{
						synchronized(o){
							size.sub();
							if(size.size() == 0){
								o.notifyAll();
							}
							//System.out.println("###############"+Thread.currentThread().getName() + " thread notify at "+result.getResultMap().size());
						}
					}
    			}
	    	}
	    }
	}

}



package com.cn.test1;

import java.util.HashMap;
import java.util.Map;

/**
 * 继承成功多少条,失败多少条
 *
 */
public class BatchAddWeakPasswordResult {
	
	private int successCount = 0;
	private int failsCount = 0;
	private Map<String,String> resultMap = new HashMap<String,String>();
	
	public int getSuccessCount() {
		return successCount;
	}
	public void setSuccessCount(int successCount) {
		this.successCount = successCount;
	}
	public int getFailsCount() {
		return failsCount;
	}
	public void setFailsCount(int failsCount) {
		this.failsCount = failsCount;
	}
	public Map<String, String> getResultMap() {
		return resultMap;
	}
	public void setResultMap(Map<String, String> resultMap) {
		this.resultMap = resultMap;
	}
	
	public void addSuccessCount(){
		successCount++;
	}
	
	public void addFailsCount(){
		failsCount++;
	}
	
	public void addResultMap(String key,String value,boolean isSuccess){
		synchronized(this){
			resultMap.put(key, value);
			if(isSuccess){
				addSuccessCount();
			}else {
				addFailsCount();
			}
		}
	}
}

你可能感兴趣的:(Concurrent)