Thinking in Java学习笔记 DelayQueue和Delayed接口

DelayedTask线程要实现Delayed接口的getDelay()和compareTo()方法,放入DelayQueue队列后,通过take()方法取出时,可根据compareTo方法制定的顺序来优先取出线程执行


静态类EndSentinel负责遍历所有的线程,其设置的trigger最大,最后被执行时调用ExecutorService.shutdownNow(),来结束线程的执行




package com.test.concurrent;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class DelayedQueueDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Random rand=new Random(47);
		DelayQueue queue=new DelayQueue();
		ExecutorService exec=Executors.newCachedThreadPool();
		for(int i=0;i<20;i++){
			queue.put(new DelayedTask(rand.nextInt(5000)));
		}
		queue.add(new DelayedTask.EndSentinel(5000, exec));
		exec.execute(new DelayedTaskConsumer(queue));
	}

}

class DelayedTask implements Delayed, Runnable{
	private static int counter=0;
	private final int id=counter++;
	private final int delta;
	private final long trigger;
	protected static List sequence=new ArrayList();
	public DelayedTask(int delayInMilliseconds){
		delta=delayInMilliseconds;
		trigger=System.nanoTime()+java.util.concurrent.TimeUnit.NANOSECONDS.convert(delta,java.util.concurrent.TimeUnit.MILLISECONDS);
		sequence.add(this);
	}
	@Override
	public int compareTo(Delayed o) {
		// TODO Auto-generated method stub
		DelayedTask that=(DelayedTask)o;
		if(this.trigger>that.trigger)
			return 1;
		if(this.trigger dq;
	public DelayedTaskConsumer(DelayQueue q){
		dq=q;
	}
	@Override
	public void run(){
		try {
			while(!Thread.interrupted()){
				dq.take().run();
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("finished delayedtask consume!!!!!");
	}
}


你可能感兴趣的:(并发多线程)