Search⌘ K
AI Features

Ordered Printing

Explore how to control execution order among Java threads to print "First", "Second", and "Third" sequentially. Understand how to apply wait-notify mechanisms and CountDownLatch to coordinate threads, ensuring proper synchronization and avoiding race conditions in multithreaded environments.

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 ...