Print sequence of number using multiple threads (java)

output :

 

Thread 1: 1
Thread 2: 2
Thread 3: 3
Thread 1: 4
Thread 2: 5
Thread 3: 6
Thread 1: 7
Thread 2: 8
Thread 3: 9
Thread 1: 10

 

 

 

 

 

package snippet;

public class PrintSequenceMultiThreaded {

	public static class Printer {
		private Integer printNumber;
		private Integer maxCount;
		private Integer currentThread;
		private Integer threadCount;
		private boolean stop = false;

		public Printer(Integer printNumber, Integer maxCount, Integer currentThread, Integer threadCount) {
			super();
			this.printNumber = printNumber;
			this.maxCount = maxCount;
			this.currentThread = currentThread;
			this.threadCount = threadCount;
		}

		public synchronized void print(int threadNumber) throws InterruptedException {
			while (true) {

				while (!stop && !this.currentThread.equals(threadNumber)) {
					this.wait(1000);
				}
				if (stop) {
					this.notifyAll();
					return;
				}

				if (printNumber > maxCount) {
					stop = true;
					return;
				}
				   
                                Thread.sleep(100);
				System.out.println(Thread.currentThread().getName() +": "+ printNumber);
				printNumber++;
			
				currentThread = (currentThread + 1) % threadCount;
				this.notifyAll();
			}
		}
	}

	static class PrinterTask implements Runnable {

		private Printer printer;
		private int task;

		public PrinterTask(Printer printer, int task) {
			super();
			this.printer = printer;
			this.task = task;
		}

		@Override
		public void run() {
			try {
				printer.print(task);
			} catch (InterruptedException e) {
				System.out.println("Stopping: " + Thread.currentThread().getName());
			}

		}

	}

	public static void main(String[] args) {
		Printer printer = new Printer(1, 10, 0, 3);
		PrinterTask task1 = new PrinterTask(printer, 0);
		PrinterTask task2 = new PrinterTask(printer, 1);
		PrinterTask task3 = new PrinterTask(printer, 2);

		new Thread(task1,"Thread 1").start();
		new Thread(task2,"Thread 2").start();
		new Thread(task3,"Thread 3").start();


	}

}

3 threads to print alternate values in sequence (java)

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();
				}
			}

		}
	}

}