Java concurrent programming -32-queue-delayqueue__ algorithm with delay elements

Source: Internet
Author: User

First, Delayqueue

Elements with activation dates can be stored, and elements of future dates are ignored when the calling method returns or extracts elements from the queue

Package concurrencycollection;

Import java.util.Date;
Import java.util.concurrent.Delayed;
Import Java.util.concurrent.TimeUnit;

Public class Event2 implements delayed {

	private Date startdate;

	Public Event2 (Date startdate) {
		this.startdate = startdate;
	}

	@Override public
	int compareTo (delayed o) {
		//TODO auto-generated method Stub-
		long result = This.getdelay ( Timeunit.nanoseconds)
				-O.getdelay (timeunit.nanoseconds);
		if (Result < 0)
			return-1;
		else if (Result > 0) return
			1;
		return 0;
	}

	/**
	 * Returns the time remaining for the activation date, specified by the parameter *
	 *
	@Override public
	long Getdelay (timeunit unit) {
		//TODO auto-generated method Stub
		Date nowdate = new Date ();
		Long diff = startdate.gettime ()-nowdate.gettime ();
		return Unit.convert (diff, timeunit.milliseconds);
	}



Package concurrencycollection;

Import java.util.Date;
Import Java.util.concurrent.DelayQueue;

public class Task2 implements Runnable {

	private int id;
	Private delayqueue<event2> Delayqueue;

	public Task2 (int id, delayqueue<event2> delayqueue) {
		this.id = ID;
		This.delayqueue = Delayqueue;
	}

	@Override public
	Void Run () {
		date nowdate = new Date ();
		Date delay = new Date ();
		Delay.settime (Nowdate.gettime () + ID * 1000);
		System.out.println ("Thread" + ID + "" + delay);
		
		for (int i = 0; i < i++) {
			Event2 event2 = new Event2 (delay);
			Delayqueue.add (Event2);}}


Package concurrencycollection;
Import Java.util.Date;
Import Java.util.concurrent.DelayQueue;

Import Java.util.concurrent.TimeUnit; public class Main3 {public static void main (string[] args) {delayqueue<event2> delayqueue = new delayqueue<
		Event2> ();

		thread[] threads = new THREAD[5];
			for (int i = 0; i < threads.length i++) {Task2 task2 = new Task2 (i + 1, delayqueue);
		Threads[i] = new Thread (TASK2);
		for (int i = 0; i < threads.length i++) {Threads[i].start ();
			for (int i = 0; i < threads.length; i++) {try {threads[i].join ();
			catch (Interruptedexception e) {e.printstacktrace ();
			} do {int counter = 0;
			Event2 Event2;
				do {event2 = Delayqueue.poll ();
			if (event2!= null) counter++;
			while (Event2!= null);
			System.out.printf ("At%s for you have read%d events\n", New Date (), counter);
			try {TimeUnit.MILLISECONDS.sleep (500); catch (Interruptedexception e) {E.PRintstacktrace ();
	} while (Delayqueue.size () > 0);
 }
}



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.