Search code examples
javamultithreadingthread-synchronization

Implementing thread waiting


I need to write a java program that creates a thread to print integers 1 - 3 inclusive. When 2 is printed, the thread waits until a second thread has finished before printing 3. The second thread prints the letter z three times.

possible outputs include: 1z2zz3 z1z2z3 12zzz3

How can I wait for a thread to finish?


Solution

  • Here is a solution for your task. With synchronized and wait you can solve this.

            public static void main(String[] args) {
                   new PrintingTheads().doJob();
             }
    
          }
    
    
    
        class PrintingTheads{
            private    Object objsynch= new Object();
            private  int numberofZ = 0;
    
            public void doJob(){
                new Thread(){
                    public void run(){
    
                        while(true){
                          System.out.print(1); Thread.yield(); // thread yeald is here for better "mixing the number 1,2 and Z letters"
                           System.out.print(2);Thread.yield();
                           synchronized (objsynch) {
                                while(numberofZ!=3) try{objsynch.wait(10);} catch(InterruptedException e){}
    
                            }
    
                           System.out.println(3);
                           try{Thread.sleep(1000);} catch(InterruptedException e){}  // * this part is only for easy to see what happened and can be deleted
                           synchronized (objsynch) {
                              numberofZ = 0;
                              objsynch.notifyAll();
                            }
                        }
    
                        }
    
                 }.start();
    
    
                 new Thread(){
                     public void run(){
    
                         while(true){
                            synchronized (objsynch) {
                                while(numberofZ!=0) try{objsynch.wait(10);} catch(InterruptedException e){}
                            }
                            for(int i= 0;i<3;i++) {System.out.print('z');Thread.yield();}
    
                            synchronized (objsynch) {
                              numberofZ=3;
                              objsynch.notifyAll();
                            }
                         }
                     }
    
                 }.start();
            }
        }