Java program for print sequence multithreading

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

Advertisement

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 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 three threads sequentially in java
  • sequence program in java using thread

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

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

Code

Step 1: Create a thread class by extending runnable

  • Create thread class by implementing Runnable interface
  • create variable to store number and remainder
  • Create an object called lock for purpose of synchronization between multiple threads.
  • Override run method, wait until the remainder is equal to the remainder of your sequence thread class, put a wait on the lock object
  • Once the condition met
    • Print the number
    • increment the number
    • 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]

Advertisement

Leave a Reply

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

one × 3 =

Share on Social Media