THREADS AND LOCKS
Locks and Synchronization
17.13
Finally, suppose that
to
and
fro
are both
synchronized
:
class SynchSynchSimple {
int a = 1, b = 2;
synchronized void to() {
a = 3;
b = 4;
}
synchronized void fro() {
System.out.println("a= " + a + ", b=" + b);
}
}
In this case, the actions of method
fro
cannot be interleaved with the actions
of method
to
, and so
fro
will print either
a=1, b=2
or
a=3, b=4
.
17.12 Threads
They plant dead trees for living, and the dead
They string together with a living thread . . .
But in no hush they string it . . . With a laugh, . . .
They bring the telephone and telegraph.
Robert Frost,
The Line gang
(1920)
Threads are created and managed by the built in classes
Thread
( 20.20) and
ThreadGroup
( 20.21). Creating a
Thread
object creates a thread and that is the
only way to create a thread. When the thread is created, it is not yet active; it
begins to run when its
start
method ( 20.20.14) is called.
Every thread has a
priority
. When there is competition for processing
resources, threads with higher priority are generally executed in preference to
threads with lower priority. Such preference is not, however, a guarantee that the
highest priority thread will always be running, and thread priorities cannot be used
to reliably implement mutual exclusion.
17.13 Locks and Synchronization
There is a lock associated with every object. The Java language does not provide a
way to perform separate
lock
and
unlock
actions; instead, they are implicitly per
formed by high level constructs that arrange always to pair such actions correctly.
(We note, however, that the Java Virtual Machine provides separate
monitorenter
and
monitorexit
instructions that implement the
lock
and
unlock
actions.)
415
footer
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