threads

Unbounded work queue example

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: June 30th, 2013
0 81 2 minutes read

This is an example of an unbounded work queue. The steps of creating and using an unbounded work queue are described below:

  • We have created a class,WorkQueue that has aLinkedList of Objects and two methods insynchronized statement. The first one isaddWork(Object o) and appends an object to the end of the list, usingaddLast(Object o) API method of LinkedList. The second method, isgetWork() and removes and returns the first element of the list, usingremoveFirst() API method of LinkedList. If the list is empty, it keeps waiting, usingwait() API method ofObject that causes the thread invoked to this object to wait until another thread calls thenotify() API method ofObject for this object.
  • We have also created a class,Worker that extends theThread and overrides itsrun() API method. It has aWorkQueue object and in itsrun() method it callsgetWork() method ofWorker forever.
  • We create a new instance ofWorkQueue and use it to create two newWorker threads.
  • We begin the threads’ execution, usingstart() API method of Thread, so the threads start retrieving elements from the list, and we also add elements to the list, callingaddWork(Object o) method of Worker.

Let’s take a look at the code snippet that follows: 

package com.javacodegeeks.snippets.core;import java.util.LinkedList;public class Main {    public static void main(String[] argv) {    WorkQueue workQueue = new WorkQueue();  int numthreads = 2;    Worker[] workers = new Worker[numthreads];    for (int i = 0; i < workers.length; i++) {workers[i] = new Worker(workQueue);workers[i].start();  }  for (int i = 0; i < 100; i++) {workQueue.addWork(i);  }    }}class WorkQueue {    LinkedList<Object> workQueue = new LinkedList<Object>();    public synchronized void addWork(Object o) {  workQueue.addLast(o);  notify();    }    public synchronized Object getWork() throws InterruptedException {  while (workQueue.isEmpty()) {wait();  }  return workQueue.removeFirst();    }}class Worker extends Thread {    WorkQueue queue;    Worker(WorkQueue queue) {  this.queue = queue;    }    @Override    public void run() {  try {while (true) {        Object x = queue.getWork();    if (x == null) {  break;    }    System.out.println(x);}  } catch (InterruptedException ex) {  }    }}

Output:

123456879101112141315161718...

 
This was an example of an unbounded work queue in Java.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: June 30th, 2013
0 81 2 minutes read
Photo of Ilias Tsagklis

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.

Related Articles

Bipartite Graph

Return a value from a thread

November 11th, 2012
Bipartite Graph

Java Thread Join Example

February 9th, 2015
Bipartite Graph

Java Daemon Thread Example

November 3rd, 2014
Bipartite Graph

CountDownLatch example

November 11th, 2012
Bipartite Graph

BlockingQueue example

November 11th, 2012
Bipartite Graph

CyclicBarrier example

November 11th, 2012
Bipartite Graph

Bounded work queue example

November 11th, 2012
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.