20-CS-4003-001 Organization of Programming Languages Fall 2017
Hints for Lab Assignment 2

Lambda calculus, Type theory, Formal semantics, Program analysis

Avoid Deadlock

    Either the girl or the boy enters ping first. Say it's the girl. Have her wait (see syntax below for wait). When the boy enters his ping, have him wake up the girl (see syntax below for notify) and then wait. Now the girl can continue in her ping to invoke the confirm of the boy. Since the boy is waiting, the girl's thread is not blocked by synchronized. The girl completes the boy's confirm but must wake up the boy before exiting the ping. The girl does so then exits. Now the awake boy continues in his ping to invoke the girl's confirm. The boy is not blocked as the girl is done (not executing ping) so the boy successfully completes the call to the girl's confirm. The boy should now attempt to wake up the girl which he would have to do if he were the first one to enter ping. There is no sleeping girl but that is OK - no harm done - and the boy terminates.
 
Semantics of 'synchronized'
    Consider a class X with several synchronized methods. Let a be an object of class X and let b be a second object of class X. Any thread t that enters one of the synchronized methods of X via object a blocks all other threads from entering synchronized methods of X via a but has no effect on threads attempting to enter synchronized methods of X via object b. The blocking influence of t lasts as long as t is executing in a synchronized method obtained through a and as long as t is not waiting in that method.
 
Syntax of 'wait'
   
synchronized void function () {
   ...
   try { wait(); } catch (Exception e) {}
	...
}
A thread entering the try-catch block will wait there until notified.
 
Syntax of 'notify'
   
class Stuff {
   synchronized void function_1 () {
      ...
      try { wait(); } catch (Exception e) {}
	   ...
   }

   synchronized void function_2 () {
      ...
      notify();
      ...
   }
}
A thread entering the try-catch block of function_1 will wait there until notified. A second thread entering function_2 will invoke notify and wake up the sleeping thread in function_1. The function_1 thread does not proceed until the function_2 thread has exited that function. Then the function_1 thread continues to completion of function_1.
 
Link suggested by Riley Jackson
    https://www.javatpoint.com/inter-thread-communication-example