Print sequence using 3 threads in java

Print sequence using 3 threads in java

Table of Contents

In this post, we will see how to print sequence using 3 threads in java.It is similar to printing odd-even numbers using two threads in java.

Problem

You are given 3 threads. You need to print sequence using these 3 threads.You need to print in natural order up to MAX.

For example:
Let’s say you have 3 threads. T1,T2 and T3.
If MAX is 10, you need to print:

T1 1
T2 2
T3 3
T1 4
T2 5
T3 6
T1 7
T2 8
T3 9
T1 10

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.

Let me provide simple program for it.
Let’s create a class named "PrintSequenceRunnable" which will implement the Runnable interface.

Let’s create a class named "PrintThreadsSequentiallyMain" which will implement the Runnable interface.

When you run above program, you will get below output.

T1 1
T2 2
T3 3
T1 4
T2 5
T3 6
T1 7
T2 8
T3 9
T1 10

That’s all about printing threads using 3 threads in java.


import_contacts

You may also like:


import_contacts

You may also like:

Related Posts

  • Get Thread Id in Java
    30 September

    Get Thread Id in Java

    Table of ContentsGet Thread Id in JavaGet Thread Id of Current Running ThreadGet Thread id of Multiple Threads In this article, we will learn to get thread id of a running thread in Java. An Id is a unique positive number generated at the time of thread creation. This id remains unchanged during the lifetime […]

  • ArrayBlockingQueue in java
    19 September

    ArrayBlockingQueue in java

    Table of ContentsWhat is BlockingQueueArrayBlockingQueueFeaturesConstructorsMethodsUsage ScenariosImplementation CodeSummary In this article, we will understand the Java concurrent queue, BlockingQueue. We will then go deep into it’s one of the implementation, ArrayBlockingQueue. What is BlockingQueue BlockingQueue interface was introduced in Java 5 under concurrent APIs and it represents a thread-safe queue in which elements can be added […]

  • Delay java program by few seconds
    11 December

    Java wait seconds or delay Java program for few secs

    Table of ContentsUsing Thread.sleepUsing TimeUnit.XXX.sleep methodUsing ScheduledExecutorServiceFrequently asked questions on Java wait secondsHow to wait for 5 seconds in java?How to wait for 1 seconds in java?How to pause for 5 seconds in java? In this post, we will see how to delay java program for few secs or wait for seconds for java program […]

  • 29 May

    How to print even and odd numbers using threads in java

    Table of ContentsProblemSolution 1Print even and odd numbers using threads in java Solution 2: Using remainder In this post, we will see how to print even and odd numbers using threads in java. see also: How to print sequence using 3 threads in java Problem You are given two threads. You need to print odd […]

  • wait(),notify() and notifyAll() in java
    22 October

    Why wait(), notify() And notifyAll() methods are in Object Class

    In this post, we will see why wait(), notify() And notifyAll() methods are in Object Class And Not in Thread Class. This is one of the most asked java multithreading interview questions. You might know that wait(), notify() And notifyAll() Methods are in Object class and do you know the reason for the same? Let’s […]

  • Custom BlockingQueue in java
    16 October

    Custom BlockingQueue implementation in java

    In this post, we will see how to create your own custom BlockingQueue. This is one of the most asked java interview questions. You need to implement your own BlockingQueue. This question helps interviewer to get your understanding of multithreading concepts. Here is simple implementation of BlockingQueue. We will use array to store elements in […]

Comments

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.