DelayQueue queue

package com.bjsxt.base.coll013;

import java.util.Iterator;
import java.util.concurrent.DelayQueue;

/**
 * @author Administrator
 *DelayQueue: A Queue with a delay time, in which an element can be taken from the queue only when the specified time is up.
	Objects in DelayQueue must implement the Delayed interface, there is no limit to the size, and there are many application scenarios.
	For example, the data that has timed out in the cache is removed, the task is timed out, and the idle connection is closed.
 */
public class WangBa implements Runnable {  
    
    private static DelayQueue<Wangmin> queue = new DelayQueue<Wangmin>();  
    
    public boolean yinye =true;  
      
    public void shangji(String name,String id,int money){  
        Wangmin man = new Wangmin(name, id, 1000 * money + System.currentTimeMillis());  
        System.out.println("screen name"+man.getName()+"ID card"+man.getId()+"payment"+money+"block, start on the machine...");  
        this.queue.add(man);  
    }  
      
    public void xiaji(Wangmin man){  
        System.out.println("Screen name"+man.getName()+"ID card"+man.getId()+"Time to get off the plane...");  
        System.out.println("Remaining: "+queue.size()+"Shoes with");
    }  
  
    @Override  
    public void run() {  
        while(yinye){  
            try {  
                Wangmin man = queue.take();  
                xiaji(man);  
            } catch (InterruptedException e) {  
                e.printStackTrace ();  
            }  
        }  
    }  
      
    public static void main(String args[]){  
        try{  
            System.out.println("Internet cafes are open for business");  
            WangBa siyu = new WangBa();  
            Thread shangwang = new Thread(siyu);  
            shangwang.start();  
              
            siyu.shangji("Passenger A", "123", 1);  
            siyu.shangji("Passerby B", "234", 10);  
            siyu.shangji("Passenger C", "345", 5);  
        }  
        catch(Exception e){  
            e.printStackTrace ();
        }  
  
    }  
}  

 

package com.bjsxt.base.coll013;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class Wangmin implements Delayed {  
    
    private String name;  
    //ID card  
    private String id;  
    //deadline  
    private long endTime;  
    //Define the time tool class
    private TimeUnit timeUnit = TimeUnit.SECONDS;
      
    public Wangmin(String name,String id,long endTime){  
        this.name=name;  
        this.id=id;  
        this.endTime = endTime;  
    }  
      
    public String getName(){  
        return this.name;  
    }  
      
    public String getId(){  
        return this.id;  
    }  
      
    /**
     * Used to determine whether the deadline is reached
     */  
    @Override  
    public long getDelay(TimeUnit unit) {
        //return unit.convert(endTime, TimeUnit.MILLISECONDS) - unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    	return endTime - System.currentTimeMillis();
    }  
  
    /**
     * For mutual batch comparison and sorting
     */  
    @Override  
    public int compareTo(Delayed delayed) {  
    	Wangmin w = (Wangmin)delayed;  
        return this.getDelay(this.timeUnit) - w.getDelay(this.timeUnit) > 0 ? 1:0;  
    }  
  
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}  

 

Guess you like

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