daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest Dec 16th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class WaitNNotifiyExample {
  2. public static void main(String[] args) {
  3.  String lockObject = "lock Object";
  4.  RunnerClass runnerClass_1 = new RunnerClass("First Thread", lockObject);
  5.  RunnerClass runnerClass_2 = new RunnerClass("Second Thread", lockObject);
  6.  new Thread(runnerClass_1).start();
  7.  new Thread(runnerClass_2).start();
  8.  try {
  9.   Thread.currentThread().sleep(100);//To ensure all threads in waiting stage
  10.  } catch (InterruptedException e) {
  11.   e.printStackTrace();
  12.  }
  13.  synchronized (lockObject) {
  14.   lockObject.notify();//Line number 15
  15.   //lockObject.notifyAll();
  16.  }
  17. }
  18. }
  19. class RunnerClass implements  Runnable {
  20. public String runnerName;
  21. public String locObject;
  22. public RunnerClass(String runnerName, String lockObject){
  23.  this.runnerName = runnerName;
  24.  this.locObject = lockObject;
  25. }
  26. public void run() {
  27.  try {
  28.  synchronized (locObject) {
  29.   System.out.println("The thread "+runnerName+" before wait executing");
  30.   locObject.wait();
  31.   System.out.println("The thread "+runnerName+" got notified");  
  32.  }
  33.  } catch (InterruptedException e) {
  34.   e.printStackTrace();
  35.  }
  36.  
  37. }
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top