17
Threads and Locks
THREADS AND LOCKS
consumes computational effort, a thread can suspend itself using
wait
 until such
time as another thread awakens it using
notify
. This is especially appropriate in
situations where threads have a producer consumer relationship (actively cooper 
ating on a common goal) rather than a mutual exclusion relationship (trying to
avoid conflicts while sharing a common resource).
As a thread executes code, it carries out a sequence of actions. A thread may
use
 the value of a variable or
assign
 it a new value. (Other actions include arith 
metic operations, conditional tests, and method invocations, but these do not
involves variables directly.) If two or more concurrent threads act on a shared vari 
able, there is a possibility that the actions on the variable will produce timing 
dependent results. This dependence on timing is inherent in concurrent program 
ming, producing one of the few places in Java where the result of executing a pro 
gram is not determined solely by this specification.
Each thread has a working memory, in which it may keep copies of the values
of variables from the main memory that is shared between all threads. To access a
shared variable, a thread usually first obtains a lock and flushes its working mem 
ory. This guarantees that shared values will be thereafter be loaded from the
shared main memory to the threads working memory. When a thread unlocks a
lock it guarantees the values it holds in its working memory will be written back
to the main memory.
This chapter explains the interaction of threads with the main memory, and
thus with each other, in terms of certain low level actions. There are rules about
the order in which these actions may occur. These rules impose constraints on any
implementation of Java, and a Java programmer may rely on the rules to predict
the possible behaviors of a concurrent Java program. The rules do, however, inten 
tionally give the implementor certain freedoms; the intent is to permit certain stan 
dard hardware and software techniques that can greatly improve the speed and
efficiency of concurrent code.
Briefly put, these are the important consequences of the rules:
Proper use of synchronization constructs will allow reliable transmission of
values or sets of values from one thread to another through shared variables.
When a thread uses the value of a variable, the value it obtains is in fact a
value stored into the variable by that thread or by some other thread. This is
true even if the program does not contain code for proper synchronization. For
example, if two threads store references to different objects into the same ref 
erence value, the variable will subsequently contain a reference to one object
or the other, not a reference to some other object or a corrupted reference
value. (There is a special exception for
long
 and
double
 values; see  17.4.)
400






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