Java Reference
In-Depth Information
wait();
numResources--;
//'Wake up' waiting producer…
notify();
}
catch (InterruptedException interruptEx)
{
System.out.println(interruptEx);
}
return numResources;
}
}
The ClientThread objects created by ResourceServer handle all resource requests
from their respective clients. In this simplifi ed example, clients will be allowed to
request only one item at a time from the resource 'pile', which they will do simply
by sending a '1'. When a client wishes to disconnect from the service, it will send a
'0'. The code for ClientThread is shown below. Just as for classes Producer and
Resource , this code must be accessible by ResourceServer . Note that, although
ClientThread calls takeOne to 'consume' an item of resource on behalf of the client,
the only thing that is actually sent to the client is a symbolic message of confi rma-
tion that the request has been granted. Only when the material on serialisation has
been covered at the end of the next chapter will it be clear how general resource
'objects' may actually be sent to a client.
import java.io.*;
import java.net.*;
import java.util.*;
class ClientThread extends Thread
{
private Socket client;
private Resource item;
private Scanner input;
private PrintWriter output;
public ClientThread(Socket socket, Resource resource)
{
client = socket;
item = resource;
try
{
//Create input and output streams
//on the socket…
input = new Scanner(client.getInputStream());
output = new PrintWriter(
client.getOutputStream(),true);
}
Search WWH ::




Custom Search