threads

Worker coordination using WorkQueue

In this example we shall show you how to achieve a coordination between two threads that try to get the same objects. The steps of the example are described below:

  • We have created a class, WorkerQueue, that has a LinkedList of Objects. In its synchronized void addWork(Object o) method it appends the object to the end of the list, with addLast(Object e) API method of LinkedList and then calls notify() API method of Object to wake up the thread waiting for it. In its synchronized Object getWork() method it waits until the list is empty, calling wait() API method of Object and it uses removeFirst() API method of LinkedList to remove and return the first element of the list. Since both methods are in synchronized statement, it is not possible for two invocations of them on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
  • We have also created a class, Worker that extends the Thread. It has a WorkerQueue and it overrides the run() API method of the Thread. In this method, it sets an Object to work, calling the getWork() method of WorkerQueue forever.
  • We create a new WorkerQueue and two new Worker threads with the same WorkerQueue above created and call their start() API method to start execution. The we call addWork(Object o) method of the WorkerQueue for a number of objects. The WorkerQueue is the same for the two threads, so it coordinates the work between the two threads. The added to the list objects are removed from the list by the threads, since in their run() method they constantly try to get the objects,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

import java.util.LinkedList;


public class CoordinationExampe {

    public static void main(String[] argv) {

  

  WorkerQueue workqueue = new WorkerQueue();


  int threads = 2;

  

  Worker[] workth = new Worker[threads];

  for (int i = 0; i < workth.length; i++) {


workth[i] = new Worker(workqueue);


workth[i].start();

  }


  for (int i = 0; i < 100; i++) {


workqueue.addWork(i);

  }
    }
}

class WorkerQueue {

    LinkedList<Object> q = new LinkedList<Object>();

    public synchronized void addWork(Object obj) {

  q.addLast(obj);

  notify();
    }

    public synchronized Object getWork() throws InterruptedException {

  while (q.isEmpty()) {


wait();

  }

  

  return q.removeFirst();
    }
}

class Worker extends Thread {

    WorkerQueue q;

    Worker(WorkerQueue q) {

  this.q = q;
    }

    @Override
    public void run() {

  try {


while (true) {


    Object o = q.getWork();



    if (o == null) {



  break;


    }


    System.out.println(o);


}

  } catch (InterruptedException ex) {

  }
    }
}

 
This was an example of how to achieve a coordination between two threads that try to get the same objects in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button