Java Reference
In-Depth Information
response = networkInput.nextLine();
//Display server's response to user…
System.out.println(
"\nSERVER> " + response);
}while (!message.equals("QUIT"));
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
fi nally
{
try
{
System.out.println(
"\nClosing connection…");
socket.close();
}
catch(IOException ioEx)
{
System.out.println(
"Unable to disconnect!");
System.exit(1);
}
}
}
}
If you wish to test the above application, you should start the server running in
one command window and then start up two clients in separate command windows.
Sample output from such an arrangement is shown in Fig. 3.3 .
3.4
Locks and Deadlock
As mentioned at the start of Sect. 3.2 , writing multithreaded programs can present
some awkward problems, primarily caused by the need to coordinate the activities
of the various threads that are running within an application. In order to illustrate
what can go wrong, consider the situation illustrated in Fig. 3.4 , where thread1 and
thread2 both need to update a running total called sum .
If the operation that each thread is trying to execute were an atomic operation
(i.e., one that could not be split up into simpler operations), then there would be
no problem. Though this might at fi rst appear to be the case, this is not so. In order
to update sum , each thread will need to complete the following series of smaller
Search WWH ::




Custom Search