System.out.print(data);
                  
                  
                  
                  
                  
                          try {
                  
                  
                              Thread.sleep(1000);
                  
                  
                          } catch (InterruptedException e) {
                  
                  
                              System.out.println("Interrupted");
                  
                  
                          }
                  
                  
                      }
                  
                  
                  }
                  
                  
                  class Synch2 implements Runnable
                  
                  
                  {
                  
                  
                      String data;
                  
                  
                      Synch target;
                  
                  
                      Thread t;
                  
                  
                      public Synch2(Synch targ,String a ) {
                  
                  
                          target = targ;
                  
                  
                          data = a;
                  
                  
                          t = new Thread(this);
                  
                  
                          t.start();
                  
                  
                  
                  
                  
                      }
                  
                  
                      public void run()
                  
                  
                      {
                  
                  
                          target.method(data);
                  
                  
                      }
                  
                  
                  }
                  
                  
                  class Main
                  
                  
                  {
                  
                  
                      public static void main(String args[])
                  
                  
                      {
                  
                  
                          Synch target = new Synch();
                  
                  
                  
                  
                  
                          Synch2 obj1 = new Synch2(target, "Hello !!\n");
                  
                  
                          Synch2 obj2 = new Synch2(target, "Welcome");
                  
                  
                          Synch2 obj3 = new Synch2(target, " to Computer Department\n");
                  
                  
                  
                  
                  
                          try
                  
                  
                          {
                  
                  
                              obj1.t.join();
                  
                  
                              obj2.t.join();
                  
                  
                              obj3.t.join();
                  
                  
                  
                  
                  
                          }
                  
                  
                          catch(InterruptedException r)
                  
                  
                          {
                  
                  
                              System.out.println("Intruppted");
                  
                  
                          }
                  
                  
                  
                  
                  
                      }
                  
                  
                  }
                  
                  
                  
                  
                  
                  why the output differs each time i run it ?
                  
                  
                  
                  
                  
                  i want output as :
                  
                  
                  Hello !
                  
                  
                  Welcome to Computer Department
                  
                  
                
Here's first google result https://www.baeldung.com/java-countdown-latch
Обсуждают сегодня