Java Reference
In-Depth Information
Using a Web service can be quite convenient; however, you must know the
format of the request and of the returned data and be certain that the Web
service will continue to be available. If it is unavailable for any reason, some
functionality of your program will be disabled.
Synchronizing Multithreaded Code on an Object
Recall that servlets are multithreaded, meaning the same code may be
executed concurrently by different threads. Each thread gets a separate copy of
local variables, meaning those variables are thread-safe by definition. Code that
modifies a shared resource, such as data in a database or an instance variable,
however, must be given mutually exclusive access to that resource, thereby mak-
ing the code thread-safe. Additionally, if the value of data can change, code
accessing that data also should be made thread-safe.
The synchronized keyword provides thread-safe code by locking access to
an object within a method or block of code. Java associates a lock, or monitor ,
with every object. Before the code in a synchronized block or method, the com-
piler automatically inserts the necessary code to acquire the monitor for an
object and code to release the monitor before the method returns or the block
ends. To declare a method as synchronized, place the synchronized keyword
before the return type, as in
public synchronized void myLockedMethod()
Any method may be declared synchronized. Constructors, however, do not need
to be synchronized because they only execute when creating an object, which
only can happen in one thread for any given new object. A block of code is
synchronized by using the synchronized statement, as in
synchronized (expression)
{
statement(s);
}
where expression is the object to lock. Although any object can be used for the
monitor, the monitor object typically is an object used within the synchronized
code.
Synchronization can be difficult to understand. As an analogy, let three
motorcycles represent three methods or blocks of code, three people represent
three threads, and one helmet represent the object providing the lock. Only the
person with the helmet can operate one of the three motorcycles, so the motor-
cycles are synchronized on the helmet. Without the helmet, the other two people
(threads) must wait to use one of the remaining motorcycles, even though they
are not in use. When the first person returns the helmet, the next person may
use it with any of the synchronized motorcycles. In this analogy, waiting for a
helmet makes the motorcycles thread-safe; that is, it limits the use of any of the
motorcycles to one person (thread) at a time.
All of the variables used by the remaining methods, including the method
parameters, are local variables and, therefore, are thread-safe. The exception is
the StockTrackerDB object, db, which is an instance variable. Additionally, this
object reads from and writes to the database. For these reasons, these methods,
Search WWH ::




Custom Search