|
Delayqueue-I am/not complete
In some cases, objects stored in the queue need to be placed in another queue for a period of time before they are ready to be taken out. You can use the java. util. Concurrent. delayqueue class to implement the blockingqueue interface. Delayqueue requires that the queue object be resident on the queue for a specified period of time.
The real example I want to use to prove it (which may be what you're eager for) is about muffins ). Oh, Muffin object (like the Java we are talking about-there is no intention of coffee double-off ). Suppose you have a delayqueue and put some muffin objects in it. The Muffin object (as shown below) must implement the java. util. Concurrent. Delayed interface so that it can be placed in delayqueue. This interface requires the muffin object to implement the getdelay method (as shown below ). The getdelay method actually declares how long the object will be stored in delayqueue. When the value returned by this method is 0 or less than 0, the object is ready (or in this example, It is baked) and can be retrieved (see listing 3 ).
The Muffin class also implements the compareto (Java. util. Concurrent. Delayed) method. Because the delayed interface inherits from the java. Lang. Comparable class, this Convention restricts the bakecompletion time of the muffin object.
Because you don't really want to eat not fully baked muffin, you need to store the recommended baking time in delayqueue. Listing 4, taken from the delayqueueusageexample class, shows the enqueue and dequeue muffin objects from delayqueue.
As you can see, the baking time for the muffin object is set using its constructor (the constructor expects the baking time to be in seconds ).
As mentioned above, a muffin object is not allowed to be taken out in delayqueue until its delay time (also called baking time) expires. The element is extracted from the queue based on the earliest delay time. In this example, if you have some muffin objects that have been baked, they will be taken out based on how long they have waited (in other words, the earliest muffin will be taken out before the newly baked muffin ).
Synchronousqueue
In Java 1.5, another way to block the queue is synchronousqueue. Interestingly, this queue has no internal capacity. This is intentional, because queue is intended to be used for delivery purposes. This means that in a synchronous queue structure, the put request must wait for the take request from another thread to synchronousqueue. At the same time, a take request must wait for a put request from another thread to synchronousqueue. Use a program to sample this concept. For more information, see sample code. Similar to the forward blockingqueue example, it contains a consumer (synchconsumer), see listing 5.
The code in listing 5 uses the poll (long timeout, timeunit unit) method of the synchronousqueue class. This method allows the poll process to wait for a specified time (in this example, 20 seconds) Before waiting for another consumption thread to write synchronousqueue ).
In Listing 6, the producer (synchproducer) uses similar offer (e o, long timeout, timeunit unit) Methods to place objects in synchronousqueue. This method allows you to wait for a while before waiting for another thread to read synchronousqueue (in this example, 10 seconds ).
Testsynchqueue shows the actions of producer and consumer:
import java.util.concurrent.SynchronousQueue;import java.util.concurrent.LinkedBlockingQueue;public class TestSynchQueue{ public static void main(String args[]) { SynchronousQueue<String> synchQueue = new SynchronousQueue<String>(); SynchProducer producer = new SynchProducer("ProducerA",synchQueue, System.out); SynchConsumer consumerA = new SynchConsumer("ConsumerA", synchQueue, System.out); consumerA.start(); producer.start(); }}
When trying to understand the concept behind synchronousqueue, remember where these queue are usually used. The synchronized queue in javadoc indicates:
"They [synchronize queue] are suitable for the transfer design, where objects running in one thread must be synchronized with objects running in another thread so that they can be handed over to some information, time, or task. "
Author profile: Kulvir Singh bhogal is a consultant for IBM who plans and implements a Java-centric solution on the customer's website. |