Java Collections Interface- BlockingQueue Interface

Java Collections Interface- BlockingQueue Interface


Posted in : Java Posted on : December 10, 2010 at 5:42 PM Comments : [ 0 ]

A blocking queue is a queue that blocks when you try to dequeue from it and the queue is empty,

Java Collections Framework- BlockingQueue Interface

A blocking queue is a queue that blocks when you try to dequeue from it and the queue is empty, or if you try to enqueue items to it and the queue is already full. A thread trying to dequeue from an empty queue is blocked until some other thread inserts an item into the queue. A thread trying to enqueue an item in a full queue is blocked until some other thread makes space in the queue, either by dequeuing one or more items or clearing the queue completely.
Java 5 comes with blocking queue implementations in the java.util.concurrent package. Yet it can be useful to know the theory behind their implementation.
Notice how notifyAll() is only called from enqueue() and dequeue() if the queue size is equal to the size bounds (0 or limit). If the queue size is not equal to either bound when enqueue() or dequeue() is called, there can be no threads waiting to either enqueue or dequeue items. 
A BlockingQueue does not accept null elements. Implementations throw NullPointerException on attempts to add, put or offer a null. A null is used as a sentinel value to indicate failure of poll operations.
A BlockingQueue may be capacity bounded. At any given time it may have a remaining Capacity beyond which no additional elements can be put without blocking. A BlockingQueue without any intrinsic capacity constraints always reports a remaining capacity of Integer. MAX_VALUE.
BlockingQueue implementations are designed to be used primarily for producer-consumer queues, but additionally support the Collection interface.
BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control. 
The following example shows a simple server class based on use of a BlockingQueue.

Example:- Client.java
package devmanuals.com;

import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Client extends Server {
	public static void main(String[] args) {
		new Client().go();
	}

	public void go() {
		final Server server = new Server();
		server.start();

		for (int i = 0; i < 2; i++) {
			final Request request = createRequest(i);
			server.accept(request);
		}
	}

	Request createRequest(final int index) {
		return new Request() {
			public void execute() {
			for (int i = 0; i <= 40; i += 20) {
			sleep((new Random().nextInt(5) + 1) * 1000);
			System.out.println(String.format(
				"request: %d completed: %d%%", index, i));
			}
			System.out.println(String.format("reqest %d completed", index));
			}

		};
	}

	private void sleep(int millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {

		}
	}
}

class Server extends Thread {
	private BlockingQueue queue = new LinkedBlockingQueue();

	public void accept(Request request) {
		queue.add(request);
	}

	public void run() {
		while (true)
			try {
				execute(queue.take());
			} catch (InterruptedException e) {
			}
	}

	public interface Request {
		void execute();
	}

	private void execute(final Request request) {
		new Thread(new Runnable() {
			public void run() {
				request.execute();
			}
		}).start();
	}
}
Output:-
request: ० completed: ०%
request: १ completed: ०%
request: १ completed: २०%
request: ० completed: २०%
request: १ completed: ४०%
reqest १    completed
request: ० completed: ४०%
reqest ०    completed

Download This Code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics