Reentrant lock and Executor Services
EmailManager Service(thread) maintains its Internal Event Queue, which makes use of synchronized blocks of events such as synchronized postEvent(method), synchronized getNextEvent(method).
Event Queue queues the events both from the underlying peer classes and from trusted application classes. Event Queue asynchronously extracts events from the queue and dispatches them by calling dispatchEvent(AWTEvent). Synchronization in java, the capability to provide any shared resource to multiple threads.
- To prevent thread interference.
- To prevent consistency problem, to what you call consumer and producer problem
Mutually Exclusive, if we can avoid two processes entering in their critical sections simultaneously, we could avoid race conditions. For instance, a process executes the shared resource, and all other processes desiring the resource at the same time moment should be kept waiting; when that process has finished executing the shared resource, one of the processes waiting; should be allowed to proceed.
- Synchronized method.
- Synchronized block.
- static synchronization.
Cooperation (Inter-thread communication in java)
Refer to figure: Thread which was pushing the Events to the queue and another one which was extracting the event out were of equal priority i.e default priority. When a task and alert thread, pushed the Email Message event to Internal Event Queue, it acquires the object lock associated with a shared resource, before it can enter in its critical section. The runtime system ensures that no other thread can enter a shared resource if another thread already holds the object lock associated with it. If a thread (Email Manager Thread) cannot immediately acquire the object lock, it is blocked, i.e., Email Manager Thread must wait for the lock to become available. Email Manager thread is blocked indefinitely waiting to enter a synchronized block because other threads are constantly allowed access before it gets, as it has an enormous amount of work to perform.