LAB-4       Threads (2) : Monitor

 

Shared variables

Using static (shared) variables to manually synchronize threads running. This variable will serve as a flag for one thread to know whether the other
is done or still running and vice versa.

Examples:

Tache.java

TestTache.java

 

Synchronized methods

Usually, several objects can execute a method at the same time. A synchronized method can only be executed by one object at a time. Examples below illustrate the concept.

Examples:

TacheNonSync.java : Thread using a method that is not synchronized.

TacheSync.java :  Thread using a method that is synchronized.

 

Static methods for mutual exclusion

As variables, a static method is a method for which only one instance exists. All the objects instantiated from the class where the method is defined will refer to the same instance. This allows the performance of mutual exclusion (using synchronized) on a method of the class since all objects will try to execute the same instance. Examples below illustrate the concept:

Examples:

TacheSyncStatic.java : synchronized AND static method.

TacheSyncNonStatic.java :  synchronized but not static method.

 

 

 

Hosted by www.Geocities.ws

1