/Users/lyon/j4p/src/net/compute/ComputeThread.java

1    package net.compute; 
2     
3    import java.io.IOException; 
4    import java.io.ObjectInputStream; 
5    import java.io.ObjectOutputStream; 
6    import java.io.Serializable; 
7    import java.net.Socket; 
8     
9    public class ComputeThread extends 
10           Thread { 
11       ObjectInputStream ois; 
12       ObjectOutputStream oos; 
13    
14       ComputeThread(Socket s) 
15               throws IOException { 
16           // read the computable object from the 
17           // object input stream. 
18           // write the answer to the object output stream. 
19           oos = 
20                   new ObjectOutputStream( 
21                           s.getOutputStream()); 
22    
23           ois = 
24                   new ObjectInputStream( 
25                           s.getInputStream()); 
26       } 
27    
28       public void run() { 
29           try { 
30               Object o = ois.readObject(); 
31               if (!(o instanceof ComputableObject)) { 
32                   System.out.println("Bad object!"); 
33                   return; 
34               } 
35               ComputableObject 
36                       co = (ComputableObject) o; 
37               Serializable ans = co.compute(); 
38               oos.writeObject(ans); 
39               oos.close(); 
40               ois.close(); 
41           } catch (Exception e) { 
42               e.printStackTrace(); 
43           } 
44       } 
45   }