01: package demo.mtclient;
02:
03: class ClientThread extends Thread implements ObserverOperations {
04: private MyServer srv;
05: private String msg;
06: private int id;
07: private int time;
08:
09: private Observer me;
10:
11: public ClientThread(MyServer _s, String _x, int _id) {
12: srv = _s;
13: msg = _x;
14: id = _id;
15: time = 1;
16: setDaemon(true);
17: }
18:
19: public void setMe(Observer obs) {
20: me = obs;
21: }
22:
23: public void run() {
24: System.out.println("ClientThread " + id + " starts");
25: int lifeTime = 10;
26: try {
27: while (lifeTime > 0) {
28: lifeTime--;
29: String a[] = srv.arryfy(msg, 5);
30: System.out.println(id + ", " + lifeTime + " to go."
31: + srv.writeMessages(a, me));
32: sleep(500);
33: }
34: } catch (org.omg.CORBA.COMM_FAILURE cf) {
35: System.out.println("Communication failure");
36: } catch (Exception e) {
37: e.printStackTrace();
38: }
39: System.out.println("thread exits...");
40: }
41:
42: public void update1(Observer o) {
43: System.out.println("Client " + id + " update1");
44: o.update2();
45: }
46:
47: public void update2() {
48: System.out.println("Client " + id + " update2");
49: }
50: }
|