17.14
Wait Sets and Notification
THREADS AND LOCKS
The
synchronized
 statement ( 14.17) computes a reference to an object; it
then attempts to perform a
lock
 action on that object and does not proceed further
until the
lock
 action has successfully completed. (A
lock
 action may be delayed
because the rules about locks can prevent the main memory from participating
until some other thread is ready to perform one or more
unlock
 actions.) After the
lock action has been performed, the body of the
synchronized
 statement is exe 
cuted. If execution of the body is ever completed, either normally or abruptly, an
unlock
 action is automatically performed on that same lock.
A
synchronized
 method ( 8.4.3.5) automatically performs a
lock
 action
when it is invoked; its body is not executed until the
lock
 action has successfully
completed. If the method is an instance method, it locks the lock associated with
the instance for which it was invoked (that is, the object that will be known as
this
 during execution of the body of the method). If the method is
static
, it
locks the lock associated with the
Class
 object that represents the class in which
the method is defined. If execution of the method's body is ever completed, either
normally or abruptly, an
unlock
 action is automatically performed on that same
lock.
Best practice is that if a variable is ever to be assigned by one thread and used
or assigned by another, then all accesses to that variable should be enclosed in
synchronized
 methods or
synchronized
 statements.
Java does not prevent, nor require detection of, deadlock conditions. Programs
where threads hold (directly or indirectly) locks on multiple objects should use
conventional techniques for deadlock avoidance, creating higher level locking
primitives that don't deadlock, if necessary.
17.14   Wait Sets and Notification
Every object, in addition to having an associated lock, has an associated
wait set
,
which is a set of threads. When an object is first created, its wait set is empty.
Wait sets are used by the methods
wait
 ( 20.1.6,  20.1.7,  20.1.8),
notify
( 20.1.9), and
notifyAll
 ( 20.1.10) of class
Object
. These methods also inter 
act with the scheduling mechanism for threads ( 20.20).
The method
wait
 should be called for an object only when the current thread
(call it
T
) has already locked the object's lock. Suppose that thread
T
 has in fact
performed
N lock
 actions that have not been matched by
unlock
 actions. The
wait
method then adds the current thread to the wait set for the object, disables the cur 
rent thread for thread scheduling purposes, and performs
N unlock
 actions to relin 
quish the lock. The thread
T
 then lies dormant until one of three things happens:
416






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Inexpensive Web Hosting  Jsp Web Hosting

Cheapest Web Hosting Jsp Hosting Cheap Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved