Programmer's Blog

Programmer's reference

Monthly Archives: October 2015

[Java] Runnable notify and wait

1. implements Runnable
2. implement the run method

public void run() {
  synchronized(this) {
     <do something here>
     notify();
  }

public void main() {
  Thread t1 = new Thread(this);
  t1.start();

  <do something there>

   synchronized(t1) {
     try {
       System.out.println("Waiting for the other thread to complete");
             t1.wait(); 
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
    }

}



}