Java Reference
In-Depth Information
3.
A thread object never directly invokes the
run
method. The JVM invokes the
run
method when it is time to execute the thread. Your class must override the
run
method
to tell the system what the thread will do when it runs.
4.
To prevent threads from corrupting a shared resource, use
synchronized
methods or
blocks. A
synchronized method
acquires a
lock
before it executes. In the case of an
instance method, the lock is on the object for which the method was invoked. In the case
of a static method, the lock is on the class.
5.
A synchronized statement can be used to acquire a lock on any object, not just
this
object, when executing a block of the code in a method. This block is referred to as a
synchronized block
.
6.
You can use explicit locks and
conditions
to facilitate communications among threads,
as well as using the built-in monitor for objects.
7.
The blocking queues (
ArrayBlockingQueue
,
LinkedBlockingQueue
,
PriorityBlockingQueue
) provided in the Java Collections Framework automati-
cally synchronize access to a queue.
8.
You can use semaphores to restrict the number of concurrent tasks that access a shared
resource.
9.
Deadlock
occurs when two or more threads acquire locks on multiple objects and each
has a lock on one object and is waiting for the lock on the other object. The
resource
ordering technique
can be used to avoid deadlock.
10.
The JDK 7's Fork/Join Framework is designed for developing parallel programs. You
can define a task class that extends
RecursiveAction
or
RecursiveTask
and exe-
cute the tasks concurrently in
ForkJoinPool
and obtains the overall solution after all
tasks are completed.
Q
UIZ
P
ROGRAMMING
E
XERCISES
Sections 30.1-30.5
*30.1
(
Revise Listing 30.1
) Rewrite Listing 30.1 to display the output in a text area,
as shown in FigureĀ 30.30.
F
IGURE
30.30
The output from three threads is displayed in a text area.
Search WWH ::
Custom Search