@@ -0,0 +1,1087 @@
+package com.sunny.grokkingalgorithms.java.threads;%0A%0Aclass Printer extends Thread%7B%0A%0A  private int val;%0A%0A  Printer(int val)%7B%0A    this.val = val;%0A  %7D%0A%0A  public void run()%7B%0A    while(true) %7B%0A      synchronized (this) %7B%0A        try %7B%0A          wait();%0A        %7D catch (InterruptedException e) %7B%0A          e.printStackTrace();%0A          break;%0A        %7D%0A        System.out.println(val);%0A      %7D%0A    %7D%0A  %7D%0A%0A%0A%7D%0A/**%0A * Created by sundas on 4/12/2018.%0A */%0Apublic class MultiThreadPrinter %7B%0A%0A  /*%0A  t1 prints 1,1,1....%0A  t2 prints 2,2,2, ...%0A  t3 prints 3,3,3....%0A  o/p: 123123123...%0A   */%0A%0A  public static void main(String%5B%5D args) %7B%0A    Printer%5B%5D printers = new Printer%5B3%5D;%0A    for(int i = 0 ; i %3C printers.length ; i++)%7B%0A      printers%5Bi%5D = new Printer(i+1);%0A    %7D%0A    for(int i = 0 ; i %3C printers.length ; i++)%7B%0A      printers%5Bi%5D.start();%0A    %7D%0A    int i = 0;%0A    while(true)%7B%0A      synchronized (printers%5Bi%253%5D)%7B%0A        printers%5Bi%253%5D.notify();%0A      %7D%0A      try %7B%0A        Thread.sleep(1000);%0A      %7D catch (InterruptedException e) %7B%0A        e.printStackTrace();%0A      %7D%0A      i++;%0A    %7D%0A  %7D%0A%0A%7D%0A

t1 prints 1,1,1, , t2 prints 2,2,2... t3 prints 3,3, ... final output should be 1,2,3,1,2,3,1,2,3,....