Ordered Printing

This problem is about imposing an order on thread execution.

Problem Statement

Suppose there are three threads t1, t2 and t3. t1 prints First, t2 prints Second and t3 prints Third. The code for the class is as follows:

public class OrderedPrinting {

    public void printFirst() {
       System.out.print("First"); 
    }
 
    public void printSecond() {
       System.out.print("Second");
    }

    public void printThird() {
       System.out.print("Third"); 
    }

}

Thread t1 calls printFirst(), thread t2 calls printSecond(), and thread t3 calls printThird(). The threads can run in any order. You have to synchronize the threads so that the functions printFirst(), printSecond() and printThird() are executed in order.

The workflow of the program is shown below:

Press + to interact

Solution

We present two solutions for this problem; one using the basic wait() & notifyAll() functions and the other using CountDownLatch.

Solution 1 In this solution, we have a class OrderedPrinting that consists of a private variable; count. The class consists of 3 functions printFirst(),printSecond() and printThird(). The structure of the class is as follows:

class OrderedPrinting {
    int count;

    public
...