1 33 34 package edu.rice.cs.util.newjvm; 35 36 import edu.rice.cs.util.Log; 37 import edu.rice.cs.util.UnexpectedException; 38 40 import java.io.Serializable ; 41 import java.rmi.*; 42 import java.rmi.server.UnicastRemoteObject ; 43 44 46 50 public abstract class AbstractSlaveJVM implements SlaveRemote, Serializable { 51 public static final int CHECK_MAIN_VM_ALIVE_SECONDS = 1; 52 53 protected static final Log _log = new Log("MasterSlave.txt", false); 54 55 58 59 protected volatile String _quitSlaveThreadName = "Quit SlaveJVM Thread"; 60 61 62 protected volatile String _pollMasterThreadName = "Poll MasterJVM Thread"; 63 64 private volatile Thread _checkMaster = null; 65 66 private final Object _slaveJVMLock = new Object (); 67 68 private volatile boolean _slaveExited = false; 69 70 private void shutdown() { 71 _log.log(AbstractSlaveJVM.this + ".shutdown() calling System.exit(0)"); 77 System.exit(0); 78 } 79 80 81 public final synchronized void quit() { 82 85 beforeQuit(); 86 87 _slaveExited = false; 88 90 Thread t = new Thread (_quitSlaveThreadName) { 92 public void run() { 93 try { 94 synchronized(_slaveJVMLock) { 96 while (! _slaveExited) { 97 _slaveJVMLock.wait(); 99 } 100 } 101 shutdown(); 102 } 103 catch (Throwable th) { 104 _log.log(this + ".quit() failed!"); 105 quitFailed(th); 106 } 107 } 108 }; 109 110 t.start(); 111 synchronized(_slaveJVMLock) { 113 _slaveExited = true; 114 _slaveJVMLock.notify(); } 116 } 117 118 119 protected void beforeQuit() { } 120 121 122 protected void quitFailed(Throwable th) { } 123 124 132 public final void start(final MasterRemote master) throws RemoteException { 133 134 if (_checkMaster != null) throw new UnexpectedException(this + ".start(...) called a second time"); 135 136 _checkMaster = new Thread (_pollMasterThreadName) { 137 public void run() { while (true) { 140 try { Thread.sleep(CHECK_MAIN_VM_ALIVE_SECONDS*1000); } 141 catch (InterruptedException ie) { } 142 try { master.checkStillAlive(); } 144 catch (RemoteException re) { quit(); } } 146 } 147 }; 148 149 150 151 _checkMaster.setDaemon(true); 152 _checkMaster.start(); 153 _log.log(_checkMaster + " created and STARTed by " + this); 154 155 handleStart(master); } 157 158 159 protected abstract void handleStart(MasterRemote master); 160 161 } 163 | Popular Tags |