DistThreadGrouppublic class DistThreadGroup extends Thread Source code from "Java Distributed Computing", by Jim Farley.
Class: DistThreadGroup
Example: 4-4
Description: A container for a set of threads distributed across the
network. |
Fields Summary |
---|
protected ThreadGroup | localGroup | protected Hashtable | remoteGroups | protected ServerSocket | incoming | protected int | localPort | static final int | hostIdx | static final int | portIdx |
Constructors Summary |
---|
public DistThreadGroup(ThreadGroup g, int port)
// Public constructors
localGroup = g;
localPort = port;
| public DistThreadGroup(int port)
localGroup = new ThreadGroup("local:" + port);
localPort = port;
| public DistThreadGroup(String rHost, int rPort, String gname, int port)
localGroup = new ThreadGroup("local:" + port);
localPort = port;
Add(gname, rHost, rPort);
|
Methods Summary |
---|
public void | Add(java.lang.String gname, java.lang.String host, int port)
RmtThreadGroup rg = new RmtThreadGroup(host, port);
remoteGroups.put(gname, rg);
| public java.lang.ThreadGroup | GetLocalGroup()
return localGroup;
| public void | Remove(java.lang.String gname)
remoteGroups.remove(gname);
| protected void | broadcastCmd(java.lang.String cmd)
Enumeration e = remoteGroups.elements();
while (e.hasMoreElements()) {
RmtThreadGroup rg = (RmtThreadGroup)e.nextElement();
try {
Socket s = new Socket(rg.getHost(), rg.getPort());
DataOutputStream os = new DataOutputStream(s.getOutputStream());
os.writeUTF(cmd);
s.close();
os.close();
}
catch (Exception ex) {
System.out.println("DistThreadGroup: Failed to " + cmd +
" group at \"" + rg.getHost() + ":"
+ rg.getPort());
}
}
| public synchronized void | resume(boolean bcast)
if (bcast)
broadcastCmd("resume");
if (localGroup != null)
localGroup.resume();
| public void | run()
try {
incoming = new ServerSocket(localPort);
}
catch (IOException ioe) {
System.out.println("Failed to bind to port " + localPort);
return;
}
while (true) {
try {
Socket peer = incoming.accept();
DataInputStream is = new DataInputStream(peer.getInputStream());
String input = is.readUTF();
if (input.compareTo("suspend") == 0)
suspend();
else if (input.compareTo("resume") == 0)
resume();
//
// Check for other messages here ("stop", "start", etc.)
// .
// .
// .
else {
System.out.println("DistThreadGroup: Received unknown command \""
+ input + "\"");
}
}
catch (IOException ioe) {
System.out.println("Network exception reading message");
}
}
| public synchronized void | suspend(boolean bcast)
if (bcast)
broadcastCmd("suspend");
if (localGroup != null)
localGroup.suspend();
|
|