ReentrantLock,Condition

 

 

 

public class ReentrantLockAndConditionTest {

    public static void main(String[] args) {
    	ReentrantLockQueue queue =new  ReentrantLockQueue();
    	for (int i = 0; i < 100; i++) {
			queue.put("a");
			String string = queue.getString();
			System.out.println(string);
		}
    	
	}


    public abstract class MessageQueue<T>{
    	private Queue<T> queue;
    	private List<FailedMessageWrap> resendList;
    	protected int resendSleepInterval = 1000 * 60 ;
    	protected int maxFailedCount = 10;
    	private Lock sendLock = new ReentrantLock();
    	private Condition sendCondition = sendLock.newCondition();
    	private Lock resendLock = new ReentrantLock();
    	private volatile boolean stopRequired ;
    	 
    	public MessageQueue(){
    		queue = new LinkedList<T>();
    		resendList = new LinkedList<FailedMessageWrap>();
    		stopRequired = false;
    		 
    		ExecutorService sendService = Executors.newFixedThreadPool(1);
    		for (int i = 0; i < 1; i++) {
    			sendService.execute(new SendTask());
    		}
    		Executors.newSingleThreadExecutor().execute(new ResendTask());
    	}
    	 
    	public void send(T message){
    		if(message == null){
    			return;
    		}
    		
    		try {
    			sendLock.lock();
    			queue.add(message);
    			sendCondition.signalAll();
    		}finally{
    			sendLock.unlock();
    		}
    		
    	}
    	 
    	public void stop(){
    		stopRequired = true;
    	}
    	 
    	protected abstract boolean doSend(T message);
    	 
    	class FailedMessageWrap{
    		private T message;
    		private int failedCount;
    		
    		FailedMessageWrap(T message){
    			this.message = message;
    			failedCount = 0;
    		}

    		public int getFailedCount() {
    			return failedCount;
    		}
     
    		public void increaseFailedCount() {
    			this.failedCount += 1;
    		}

    		public T getMessage() {
    			return message;
    		}
    		
    	}
     
    	class SendTask implements Runnable{
    		@Override
    		public void run() {
    			while(!stopRequired){
    				T message;
    				
    				try {
    					sendLock.lock();
    					message = queue.poll();
    					if(message == null){
    						try {
    							sendCondition.await();
    						} catch (Exception e) {
    							e.printStackTrace ();
    						}
    						continue;
    					}
    				}finally{
    					sendLock.unlock();
    				}
    					
    				if(!doSend(message)){
    					try {
    						resendLock.lock();
    						resendList.add(new FailedMessageWrap(message));
    					} finally{
    						resendLock.unlock();
    					}
    				}
    			
    			}
    			
    		}
    	}
    	 
    	class ResendTask implements Runnable{
    		@Override
    		public void run() {
    			while(!stopRequired){
    				
    				try {
    					Thread.sleep(resendSleepInterval);
    				} catch (InterruptedException e) {
    					e.printStackTrace ();
    				}
    				List<FailedMessageWrap> removeList = new ArrayList<FailedMessageWrap>();
    				
    				try {
    					resendLock.lock();
    					 
    					for(FailedMessageWrap messageWrap : resendList){
    						if(messageWrap.getFailedCount() > maxFailedCount){
    							removeList.add(messageWrap);
    							continue;
    						}
    						
    						T message =  messageWrap.getMessage();
    						if(!doSend(message)){
    							messageWrap.increaseFailedCount();
    						}else{
    							removeList.add(messageWrap);
    						}
    					}
    				 
    					for (FailedMessageWrap messageWrap : removeList) {
    						resendList.remove(messageWrap);
    					}
    				} finally{
    					resendLock.unlock();
    				}
    			
    			}
    		}
    	}
    	

    }
    
    
	
    public static class ReentrantLockQueue{
    	private ReentrantLock  lock = new ReentrantLock();
    	private Queue<String> queue = new LinkedList<String>();
    	public  void put(String s){
    		try{
	    		lock.lock();
	    		queue.add(s);
    		}catch(Exception e){
    			
    		}finally{
    			lock.unlock();
    		}
    	}
    	
    	public String getString(){
    		try{
    			lock.lock();
        		String poll = queue.poll();
        		return poll;
    		}catch(Exception e){
    			
    		}finally{
    			lock.unlock();
    		}
			return null;
    	}
    	
    }
	
    
    
	
	

}

 

 

 

 

 

 

 

 

 

 

 

 

 

Donate to developers 

Driven by interest, I write 免费something with joy and sweat. I hope you like my work and can support it at the same time. Of course, if you have money to support a money field (support Alipay, WeChat, and the buckle group), if you have no money to support a personal field, thank you.

 

Personal homepage : http://knight-black-bob.iteye.com/



 
 
 Thank you for your sponsorship, I will do better!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326316323&siteId=291194637
Recommended