Java Reference
In-Depth Information
to specify the number of threads to
PoolDispatcher
. The call to
System.getProperty()
returns a
String
containing the value of the “Threads” property or the default value if
the property is not defined. The string is then converted to an integer. (See the discussion
of system properties in the text below.)
2.
startDispatching()
:
lines 15-32
Spawn N - 1 threads to execute
dispatchLoop()
:
lines 17-26
For each loop iteration, an instance of an anonymous class that extends
Thread
is cre-
ated. When the
start()
method is called, the thread executes the
run()
method of this
anonymous class. The
run()
method simply calls
dispatchLoop()
, which implements
an iterative server.
Execute
dispatchLoop()
in the main thread:
lines 27-30
The original calling thread serves as the
N
th thread of the pool.
3.
dispatchLoop()
:
lines 34-46
Accept an incoming connection:
line 39
Since there are
threads can be blocked
on
servSock
's
accept()
, waiting for an incoming connection. The system ensures that
only one thread gets a
Socket
for any particular connection. If no threads are blocked
on
accept()
when a client connection is established, the new connection is queued
until the next call to
accept()
(see Section 5.4.1).
Create a protocol instance to handle new connection:
line 40
Run the protocol for the connection:
line 41
Handle exception from
accept()
:
lines 42-44
N
threads executing
dispatchLoop()
,upto
N
Since threads are reused, the thread-pool solution only pays the overhead of thread
creation
N
times, irrespective of the total number of client connections. Since we control the
maximum number of simultaneously executing threads, we can control scheduling overhead.
Of course, if we spawn too few threads, we can still have clients waiting a long time for
service; therefore, the size of the thread pool should be tuned so that client connection time
is minimized.
In
PoolDispatcher.java
, we used system properties to specify the number of threads in
the pool. Here, we give a brief description of how system properties work.. The
System
class
contains a
Properties
instance that holds a set of externally defined property/value pairs (e.g.,
class path and JVM version). We can also define our own properties. For example, we might
want to know a user's favorite color. We could place this information in the “user.favoritecolor”
property. The following code demonstrates how to fetch and print out all system properties,
using the
getProperties()
method of
System
, and how to find a particular property value,
using
System
.
getProperty()
. The second parameter to
getProperty()
specifies a value (“None”),
to be used if the property is not found. (See
ListProperties.java
on the topic's Web site for a
complete example.)
System.getProperties().list(System.out); // Print all System properties
System.out.println("\nFavorite Color: " + // Print favorite color property
System.getProperty("user.favoritecolor", "None"));
Search WWH ::
Custom Search