Print sequence from 1 to n using three threads.
Output should be like below:
Printing output for Thread: 0 1
Printing output for Thread: 1 2
Printing output for Thread: 2 3
Printing output for Thread: 0 4
Printing output for Thread: 1 5
Printing output for Thread: 2 6
Printing output for Thread: 0 7
Printing output for Thread: 1 8
Printing output for Thread: 2 9
Printing output for Thread: 0 10
Solution is using atomic variables , without using synchronization:
package snippet; import java.util.concurrent.atomic.AtomicInteger; /** Threads are busy looping, which will lead to 100% CPU usage. You should synchronize the threads instead. **/ public class ThreeThreadsOrderedLockLess { AtomicInteger sharedOutput = new AtomicInteger(0); public static void main(String args[]) { ThreeThreadsOrderedLockLess t = new ThreeThreadsOrderedLockLess(); ThreadTasks t1 = t.new ThreadTasks(0); ThreadTasks t2 = t.new ThreadTasks(1); ThreadTasks t3 = t.new ThreadTasks(2); Thread ts1 = new Thread(t1); Thread ts2 = new Thread(t2); Thread ts3 = new Thread(t3); ts1.start(); ts2.start(); ts3.start(); } private class ThreadTasks implements Runnable { private final int threadPosition; public ThreadTasks(int threadPosition) { super(); this.threadPosition = threadPosition; } @Override public void run() { while (sharedOutput.get() < 10) { if (sharedOutput.get() % 3 == this.threadPosition) { int value = sharedOutput.get() + 1; System.out.println("Printing output for Thread: " + this.threadPosition + " " + value); sharedOutput.incrementAndGet(); } } } } }