Java program for printing sequence using 3 threads | How to run three threads sequentially in Java

Java Interview Question | Multi-threading | Print Sequence

Java program for printing sequence using 3 threads is a very common interview question asked by interviewers to test multithreading (multiple threads) concepts in Java.

  • 3 threads to print alternate values in sequence in java
  • print 1 2 3 using 3 threads in java
  • print 1 to 10 using 3 different threads in java
  • program to create 3 threads in java
  • how to run multiple threads sequentially in java
  • sequence program in java using thread
  • write java code to print sequence using three threads.

NOTE: Read this article first before jumping over the solution, else you have to come back again. 🙂

These are companies that have asked this question in interviews

Oracle – Walmart – Goldman Sachs – Morgan Stanley – UBS – Verifone – Nucleus – MakeMyTrip

Have to create 3 threads. ie. T1, T2, T3
You need to print the sequence using these 3 threads. You need to print the sequence up to the given number using these threads in sequence.

Solution

Here is the core logic to write program for sequence using three threads. We will use concept of remainder here.

  • If number%3 == 1 then T1 will print the number and increment it else will go in the wait state.
  • If number%3 == 2 then T2 will print the number and increment it else will go in the wait state.
  • If number%3 == 0 then T3 will print the number and increment it else will go in the wait state.

Code

Step 1: Create a thread class by extending runnable

  1. Create thread class by implementing Runnable interface
  2. create variable to store number and remainder
  3. Create an object called lock for purpose of synchronization between multiple threads.
  4. Override run method, wait until the remainder is equal to the remainder of your sequence thread class, put a wait on the lock object
  5. Once the condition met
    1. Print the number
    2. increment the number
    3. notify every thread who has a wait on a lock
public class PrintSequenceRunnable implements Runnable{
 
    public int PRINT_NUMBERS_UPTO=10;
    static int  number=1;
    int remainder;
    static Object lock=new Object();
 
    PrintSequenceRunnable(int remainder)
    {
        this.remainder=remainder;
    }
 
    @Override
    public void run() {
        while (number < PRINT_NUMBERS_UPTO-1) {
            synchronized (lock) {
               while (number % 3 != remainder) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
              System.out.println(Thread.currentThread().getName() + " " + number);
              number++;
              lock.notifyAll();
            }
        }
    }
}

Step 2: Main class to create 3 threads

Each thread will have remainder, and will be responsible for printing numbers whose remainder from 3 will match.

public class PrintThreadsSequentiallyMain {
 
    public static void main(String[] args) {
 
        PrintSequenceRunnable runnable1=new PrintSequenceRunnable(1); 
        PrintSequenceRunnable runnable2=new PrintSequenceRunnable(2);
        PrintSequenceRunnable runnable3=new PrintSequenceRunnable(0);
 
        Thread t1=new Thread(runnable1,"T1");
        Thread t2=new Thread(runnable2,"T2");
        Thread t3=new Thread(runnable3,"T3");
 
        t1.start();
        t2.start();
        t3.start();   
    }
}

Summary

  • This will help you understand the concept of lock, wait and notify for threads.
  • The synchronized keyword is to make sure only one thread will have access to lock object at a time.

Complete source code is available here.

[Code reference]

Leave a Reply

Your email address will not be published. Required fields are marked *

15 + eighteen =

Recent Posts

Latest News