Java Reference
In-Depth Information
Note At line 54 we attempt to start listening on port 8080. There is no particular reason for picking this
port, other than the fact that it is not a port that is officially assigned to any other application, and therefore
there is a reasonable chance that this port will be available. However, if you are running another application
that also uses port 8080 (for example, the Tomcat Web Application Server by default uses port 8080), then
an exception ( java.net.BindException ) will be thrown. If you see that exception, just change the port
number in line 54 from 8080 to some other number, for example 9090.
The myLock object is never released, because the LockOwner thread never finishes. Because
the myLock object is never released, the thread that is waiting for myLock (in this case, the
BlockingExample thread) is blocked and never gets a chance to execute.
The main thread would continue executing, however, if it were not dependent on the
locked resource. Again, this is because blocking threads do not use CPU cycles.
Caution When talking about an application or thread pausing until it can get some I/O, many publica-
tions use the standard terminology of saying that the thread or application is blocked for I/O. According to
their terminology the LockOwner thread would be blocked until a client connects to the socket. However, as
the previous example shows, the JVM state for a thread waiting for I/O may be Thread.State.RUNNABLE ,
or it may be some other state depending on how the code for the I/O class is implemented. When debugging
your code, or when perusing other publications, you need to be aware that Java does not have a separate
state for threads that are paused for I/O.
InterruptedException
InterruptedException s typically occur when a thread has been paused (sleeping, or waiting,
or trying to join several threads) for too long, and another thread deliberately calls interrupt
on the thread to be interrupted to bring it out of that state. Think of an InterruptedException
as one thread violently shaking another thread back into consciousness. When a thread is
shaken awake in this manner, the first thing it does is execute whatever code is inside its catch
( InterruptedException ) clause if it has one.
As an example, consider if you wrote a Data class that has a lockRecord method that must
wait for a record to become available before locking it, but it does not have a timeout. After the
Data class has been approved, you are asked to create a subclass of the Data class that does
handle timeouts when locking. You could rewrite the entire locking code from scratch, or you
could create a separate thread that interrupts the thread locking of the record if the lock is not
acquired within the timeout period.
Note It is possible for a thread to be interrupted at any time, even if it has not called sleep , wait ,or
join . In such a case the thread will continue to execute normally until it calls sleep , wait , or join ,at
which time an InterruptedException will immediately be thrown from the called method.
Search WWH ::




Custom Search