When two or more threads need access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time.
The process by which this synchronization is achieved is called thread synchronization.
The synchronized keyword in Java creates a block of code referred to as a critical section. Every Java object with a critical section of code gets a lock associated with the object. To enter a critical section, a thread needs to obtain the corresponding object's lock.
This is the general form of the synchronized block statement:
We also have synchronized methods which are used as below:
A synchronized block/method ensures that a call to a method that is a member of object occurs only after the current thread has successfully entered object's monitor.
Thread synchronization also includes a topic related to
INTER THREAD COMMUNICATION
Consider the classic queuing problem, where one thread is producing some data and another is consuming it. To make the problem more interesting, suppose that the producer has to wait until the consumer is finished before it generates more data.
In a polling system, the consumer would waste many CPU cycles while it waited for the producer to produce. Once the producer was finished, it would start polling, wasting more CPU cycles waiting for the consumer to finish, and so on. Clearly, this situation is undesirable.
To avoid polling, Java includes an elegant interprocess communication mechanism via the following methods:
These methods are declared within Object. Various forms of wait( ) exist that allow you to specify a period of time to wait.
The process by which this synchronization is achieved is called thread synchronization.
The synchronized keyword in Java creates a block of code referred to as a critical section. Every Java object with a critical section of code gets a lock associated with the object. To enter a critical section, a thread needs to obtain the corresponding object's lock.
This is the general form of the synchronized block statement:
synchronized(object) {
// statements to be synchronized
}
|
synchronized void m1() {
// method body to be synchronized
}
A synchronized block/method ensures that a call to a method that is a member of object occurs only after the current thread has successfully entered object's monitor.
Thread synchronization also includes a topic related to
INTER THREAD COMMUNICATION
Consider the classic queuing problem, where one thread is producing some data and another is consuming it. To make the problem more interesting, suppose that the producer has to wait until the consumer is finished before it generates more data.
In a polling system, the consumer would waste many CPU cycles while it waited for the producer to produce. Once the producer was finished, it would start polling, wasting more CPU cycles waiting for the consumer to finish, and so on. Clearly, this situation is undesirable.
To avoid polling, Java includes an elegant interprocess communication mechanism via the following methods:
- wait( ): This method tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ).
- notify( ): This method wakes up the first thread that called wait( ) on the same object.
- notifyAll( ): This method wakes up all the threads that called wait( ) on the same object.c The highest priority thread will run first.
These methods are declared within Object. Various forms of wait( ) exist that allow you to specify a period of time to wait.
No comments:
Post a Comment