Problem Statement: Four threads need to add one element each at a time in a shared list. Each Thread will have it’s own list, say, Thread 1 will have list1 similarly Thread 2 will have list2 so on. And all threads will add to the list5.
Order in which they need to add elements: Thread 1 will add one element and after Thread 1, Thread 2 will add one element and after Thread 2, Thread 3 will add one element and after Thread 3, Thread 4 will add one element – and after Thread 4 – again Thread 1 will add one element to list 5 until they add all their elements to list5.
Code for above problem [run ThreadCoordinationSimulator.java]:
/**
* This program used to demonstrate the Thread co-ordination in which threads
* will execute their tasks in given order. for example: In below example each
* thread holds a list of integers and it has to iterate and add the elements
* from list to shared list. In a below given order – Thread 1 will add one
* element from it’s list to shared list and then wait. After compilation of
* Thread 1 – Thread 2 will add one element from it’s list to shared list and
* then wait. Similarly Thread 3 and Thread 4 will add one element in above
* mentioned order wait. Once Thread 4 – has done adding element to the shared
* list, again Thread 1 will start and add the element to the thread in above
* mentioned order.
*
* @author sayedkhadri
* @version 1.0.0
*/
public class ThreadCoordinationSimulator {
List<Integer> sharedListOfIntegers = new ArrayList<Integer>();
Lock l = new ReentrantLock();
Condition c1 = l.newCondition();
Condition c2 = l.newCondition();
Condition c3 = l.newCondition();
Condition c4 = l.newCondition();
/**
* This method will add an integer to shared list after acquiring the lock
* l, otherwise it will wait till lock is available.
*
* @param i
* integer to be added.
*/
public void add(int i) {
l.lock();
sharedListOfIntegers.add(i);
l.unlock();
}
public static void main(String[] args) {
List<Integer> l1 = Arrays.asList(1, 2, 3, 4);
List<Integer> l2 = Arrays.asList(5, 6, 7, 8);
List<Integer> l3 = Arrays.asList(9, 10, 11, 12);
List<Integer> l4 = Arrays.asList(13, 14, 15, 16);
ThreadCoordinationSimulator tester = new ThreadCoordinationSimulator();
new WorkerForStoringIntegers(l1, tester.l, tester.c4, tester.c1, tester, “Thread 1″).start();
new WorkerForStoringIntegers(l2, tester.l, tester.c1, tester.c2, tester, “Thread 2″).start();
new WorkerForStoringIntegers(l3, tester.l, tester.c2, tester.c3, tester, “Thread 3″).start();
new WorkerForStoringIntegers(l4, tester.l, tester.c3, tester.c4, tester, “Thread 4″).start();
try {
tester.l.lock();
tester.c4.signalAll();
tester.l.unlock();
} catch (Exception e) {
e.printStackTrace();
}
}
}
The Worker Thread.
/**
*
*
*
* @author sayedkhadri
* @version 1.0.0
*/
public class WorkerForStoringIntegers extends Thread {
Lock lock = null;
Condition waitForCondition = null;
Condition notifyCondition = null;
List<Integer> listOfIntegers = new ArrayList<Integer>();
ThreadCoordinationSimulator tester = null;
public WorkerForStoringIntegers(List<Integer> listOfIntegers, Lock lock, Condition waitForCondtion, Condition notifyCondtion, ThreadCoordinationSimulator tester, String threadName) {
this.lock = lock;
this.waitForCondition = waitForCondtion;
this.notifyCondition = notifyCondtion;
this.listOfIntegers = listOfIntegers;
this.tester = tester;
this.setName(threadName);
}
@Override
public void run() {
lock.lock();
try {
waitForCondition.await();
for (Integer i : listOfIntegers) {
System.out.println(Thread.currentThread().getName() + ” Adding Element: ” + i + ” to shared list.”);
tester.add(i);
notifyCondition.signalAll();//once addition is done, notify the one who is waiting.
waitForCondition.await();//wait till you are notified.
}
System.out.println(Thread.currentThread().getName() + ” Successfully Completed.”);
notifyCondition.signalAll();
lock.unlock();
} catch (Exception e) {
}
}
}
Save ThreadCoordinationSimulator.java and WorkerForStoringIntegers.java in same package and run ThreadCoordinationSimulator.java.
Let me know your views on this.