Thinking in Java Learning notes Delayqueue and delayed interfaces __java

Source: Internet
Author: User

Delayedtask thread to implement the Getdelay () and CompareTo () methods of the delayed interface, after the Delayqueue queue is taken out by the take () method, the thread execution can be prioritized according to the order of the CompareTo method.


Static Class Endsentinel is responsible for traversing all threads, setting the trigger maximum, and then calling Executorservice.shutdownnow () at the end of execution, to end the thread execution




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 R
		And=new Random (47);
		Delayqueue<delayedtask> queue=new delayqueue<delayedtask> ();
		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<delayedtask> sequence=new ArrayliSt<delayedtask> ();
		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<that.trigger) return-1;
	return 0;
	@Override public void Run () {//TODO auto-generated Method Stub System.out.println (this+ ' is running ');
	Public String toString () {return "Task:" +id+ "Delta:" +DELTA;
	Public String Summary () {return "ID:" +id+ "Delta:" +DELTA; @Override Public long Getdelay (timeunit unit) {//TODO auto-generated a stub return Unit.convert (Trigger-sy
	Stem.nanotime (), Java.util.concurrent.TimeUnit.NANOSECONDS); public static class Endsentinel extends delayedtask{private Executorservice EXec;
			Public Endsentinel (int delayinmilliseconds,executorservice exec) {super (delayinmilliseconds);
		This.exec=exec;
			public void Run () {for (Delayedtask dt:sequence) {System.out.println (dt.summary () + "");
			} System.out.print (");
			System.out.println (this+ "Calling Shutdownnow ()");
		Exec.shutdownnow ();
	}} class Delayedtaskconsumer implements runnable{private delayqueue<delayedtask> dq;
	Public Delayedtaskconsumer (delayqueue<delayedtask> q) {dq=q; @Override public void Run () {a try {while!
			Thread.interrupted ()) {Dq.take (). run ();
		The catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
	} System.out.println ("Finished delayedtask consume!!!!!"); }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.