Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: further shortened the NCE

...

Code Block
bgColor#FFcccc
public class MissedSignal implements Runnable {
  private static final Object lock = new Object();
  private static int buffer_count = 5;
  private static int number; // Selects function based on thread number	
  
  public void setThreadNumber(int num) {
    number = num;
  }

  public void run (){
    synchronized(lock) {
      try {    	  
        if(number == 1) {	  
          while(buffer_count == 0) { 
      	    lock.wait();		  
      	  }  
      	} else if(number == 2) {  	    		  
      	  while(buffer_count == 10) {	
      	    lock.wait();   	    		    	        	    
      	  }
      	} else if(number == 3) {	   
          lock.notify();      	   		  
        } 	    	  
      } catch (InterruptedException ie) {
        // Handle the exception
      }
    }	  
  }

  public static void makeThread1True(setThreadParameters(int threadNumber, int bufferCount) {
    buffer_countnumber = 0threadNumber;
  }
  buffer_count      = bufferCount;
  public static void makeThread2True() {}
    buffer_count = 10;
  }
        
  public static void main(String[] args) throws IOException, InterruptedException {    
    MissedSignal ms = new MissedSignal();
     
    makeThread1True();
    ms.setThreadNumber(1);
for(int i = 0; i < 3; i++) {    new Thread(ms).start();
     // 
Buffer count does  Thread.sleep(1000);
   not matter for third thread
    makeThread2True();
    ms.setThreadNumber(2);
    new Thread(ms).start();
   
    Thread.sleep(1000);  setThreadParameters(i + 1, i * 10); // Sets buffer count for threads in the sequence: 0, 10, 20
      
    ms.setThreadNumber(3new Thread(ms).start();
     new Thread(ms).startsleep(1000);
    }
  }
}

Note that when thread 2 goes into the wait state, the condition predicate of thread 1 becomes false. When notify() is invoked by thread 3, it can be delivered to either thread 1 or thread 2 depending on the particular Java Virtual Machine (JVM). If thread 1 is chosen to be notified, its condition turns out to be false, which terminates it. This is the required functionality, that is, any thread whose condition predicate is false must be terminated. However, if the notification is delivered to thread 2, it has no effect because its condition predicate is still true, and consequently, it goes into the wait state once again. Thread 1 continues to wait despite its condition predicate being false and is not terminated in this case.

...