Implementing an Unbounded Work Queue
A work queue is used to coordinate work between a producer and
a set of worker threads. When some work needs to be performed, the
producer adds an object containing the work information to the work
queue. One of the worker threads then removes the object from the
work queue and acts upon the information. If the queue is empty, a
worker thread will block until a new object is added to the queue.
This example declares a class that implements an unbounded work
queue using a linked list.
Here's an example that uses the WorkQueue class.
The workers in this example retrieve Integer objects
from the work queue and square the values.
This worker thread removes an integer from the work queue and
calculates its square.
In J2SE 5.0, use a BoundedQueue to implement an unbounded work
queue. See Creating a Bounded Work Queue [5.0] for an example;
construct the ArrayBoundedQueue without a capacity.
public class WorkQueue {
LinkedList queue = new LinkedList();
// Add work to the work queue
public synchronized void addWork(Object o) {
queue.addLast(o);
notify();
}
// Retrieve work from the work queue; block if the queue is empty
public synchronized Object getWork() throws InterruptedException {
while (queue.isEmpty()) {
wait();
}
return queue.removeFirst();
}
}
// Create the work queue
WorkQueue queue = new WorkQueue();
// Create a set of worker threads
final int numWorkers = 2;
Worker[] workers = new Worker[numWorkers];
for (int i=0; i<workers.length; i++) {
workers[i] = new Worker(queue);
workers[i].start();
}
// Add some work to the queue; block if the queue is full.
// Note that null cannot be added to a blocking queue.
for (int i=0; i<100; i++) {
queue.addWork(i);
}
// Add special end-of-stream markers to terminate the workers
for (int i=0; i<workers.length; i++) {
queue.addWork(Worker.NO_MORE_WORK);
}
class Worker extends Thread {
// Special end-of-stream marker. If a worker retrieves
// an Integer that equals this marker, the worker will terminate.
static final Object NO_MORE_WORK = new Object();
WorkQueue q;
Worker(WorkQueue q) {
this.q = q;
}
public void run() {
try {
while (true) {
// Retrieve some work; block if the queue is empty
Object x = q.getWork();
// Terminate if the end-of-stream marker was retrieved
if (x == NO_MORE_WORK) {
break;
}
// Compute the square of x
int y = ((Integer)x).intValue() * ((Integer)x).intValue();
}
} catch (InterruptedException e) {
}
}
}
Is it safe to squelch InterruptedException?
The code does not compile, see line 8 of WorkQueue class, which has an unchecked call to queue.addLast()
I enjoyed reading this kind of stuff. Thanks for sharing such an excellent information.SO much thanks!
In this modern and fashionable society, people are pursuing for ugg boots on sale cool, unique, stylish and innovative. Whether it is cheap boots or fashion accessories all means a lot for modern society of today. Same is the case with trendy looking ugg boots cheap. When these are Ugg Boots sale, the excitement just gets doubled. Most chic looking discount uggs are in fashion now. These are one of the favorite fashion accessories for men and women long time ago. If you have not yet tried sale uggs, it's time to own one and feel the difference it can make to your personality. These are just brilliant and fabulous ugg boots for cheap. They are most iconic and can provide you with a new feeling and enhance confidence. The quality of cheap ugg is just superior to what you have dreamt of. Today owning a new and trendy looking cheap Ugg Boots are not only meant for the wealthy people. These are now made luxurious and affordable ugg boot sale to reach out to every budget and range. You can just enjoy them by ordering uk ugg boots sale online where you get the complete satisfaction and genuine quality at best possible rates. The finish, quality and designs you get from ugg boots on sale are really astonishing and you will love them all.
sdfasdfsadfs
Short inseam jeans for men at Eddie Bauer. Shop men's quality short inseam jeans. Short jeans with inseams 20" - 29". Favorite denim with short inseams.[®$€£]2012022411401005
In response to a complaint we received under the US Digital Millennium Copyright Act, we have removed 1 result(s) from this page. If you wish, you may read the DMCA complaint that caused the removal(s) at[®$€£]2012022413450682
You're all fromcheap beats by dre different parts of the UK, from Newcastle to Guernsey, but as a band youIn-Ear Headphones settled in Manchester. What was the initial draw ofPro Headphones the music scene there for Everything Everything? Monster cablesIs there something in particular about the city that has especially invigorated the band, lyrically or otherwise?It's a really good size for bands and audiences; over-ear headphonesyou can have a scene here which isn't swallowed up over-ear headphonesright away like in London. There are so many venues, promoters and gig-going people, that it's a really great place to be a bandcheap beats by dre . We got tons of small gigs at the beginning —really useful for getting better as a band — it's the best place for that. There's also the amazing musical history of the place, which is always there inspiring andcheap beats by dre reminding you of where the bar is set for Manchester bands.It's more a question of order of songs, as long as I get a breather song every now and again I'm ok. When I first start to sing a difficult song, it's tough, but after a while I have a set way to do it and Icheap beats by dre usually manage it fine now. Having said that, "MY KZ, UR BF" is actually deceptively hard, and hasn't ever really got any easier.thinking, Tiffany EarringsI’ve often thought that tapingTiffany Bangles a miniature camera to a piece of rough wouldTiffany Banglesmake a great b-roll. We’d be able to follow it Tiffany Banglesthrough everything it goes through from the time itTiffany Bracelets’s harvested from Mother Earth until it arrives at its new home as a finished design . Can you imagine the stories each piece could tell, and all the stamps it would Tiffany Braceletshave in its passport? All the people and cultures it would have experienced on its trip, but most of all the stories it could tell.
When buying running Adidas shoes online, it will help to make a thorough search and choose a shoe that offers ankle protection and also heel support.Electronics have made our lives so simple.Tennis players need to have a strong hold of the ground even when they cut sideways during play.
A few types of sports shoes that Adidas Adidas Porsche Bounce SL offers are detailed below � � Squash shoes � These are especially designed for the game of squash.The company is dedicated to bettering its products and this Adidas Porsche Design Golf is the reason why we find that the takers for Adidas shoes are plenty.When playing a game or joining a gym, one of the first things to look into is right footwear.Most Adidas outlets carry these shoes and you can buy squash Adidas shoes online too in case your local dealer does not offer a very wide choice.This is why a tennis shoe will always have ankle support and also a very strong heel.Adidas shoes are known to be highly developed in this arena.While these electronics are welcome, they have taken away the element of exercise from our lives.They have what is known as non-marking soles, commonly called yellow soles.The answer is simple � we need to sport up our lives.They provide Adidas Porsche Design Bounce a spring heel motion, which prevents the runner Adidas Porsche Bounce S from hurting his/her back and also offer an aerodynamic shape so as to allow Adidas Porsche Bounce S2 the runner better grip and control of the surface.Adidas tennis shoes are designed keeping the sideways cut of the player in mind.Adidas is known for its tennis shoes all over the world and most of these Adidas shoes can be bought online too.Adidas is no doubt a leader in sports shoes.These shoes are designed keeping the pace of the game in mind and also given a sole that will not mark the wooden floor of the squash courts.Running shoes � These are for runners.Tennis shoes � Now here the variety gets to be a bit wider than squash shoes.