137 138 final long timeToExit = System.currentTimeMillis() + 60*1000L*minutes; 139 final int exitStatus = status; 140 141 terminator = new Thread(Terminator.class.getName()) { 142 public void run() { 143 long timeToSleep = timeToExit - System.currentTimeMillis(); 144 if (timeToSleep > 0) 145 try { 146 // 147 // Use wait() instead of sleep(), because Java 2 148 // specification doesn't guarantee the method 149 // sleep() to yield to other threads. 150 // 151 Object someDummyObject = new Object(); 152 synchronized (someDummyObject) { 153 someDummyObject.wait(timeToSleep); 154 } 155 } catch (InterruptedException exception) { 156 exception.printStackTrace(System.err); 157 // 158 // OOPS, the dagger for terminator looks broken: 159 // 160 return; 161 }; 162 // 163 // OK, lets do it now: 164 // 165 System.err.println( 166 "#\n# Terminator: prescheduled program termination.\n#"); 167 System.exit(exitStatus); // terminator to all threads 168 } 169 }; 170 171 terminator.setPriority(Thread.MAX_PRIORITY); 172 terminator.setDaemon(true); 173 terminator.start(); 174 } 175 } | 137 138 final long timeToExit = System.currentTimeMillis() + 60*1000L*minutes; 139 final int exitStatus = status; 140 141 terminator = new Thread(Terminator.class.getName()) { 142 public void run() { 143 long timeToSleep = timeToExit - System.currentTimeMillis(); 144 if (timeToSleep > 0) 145 try { 146 // 147 // Use wait() instead of sleep(), because Java 2 148 // specification doesn't guarantee the method 149 // sleep() to yield to other threads. 150 // 151 Object someDummyObject = new Object(); 152 synchronized (someDummyObject) { 153 someDummyObject.wait(timeToSleep); 154 } 155 } catch (InterruptedException exception) { 156 exception.printStackTrace(System.err); 157 return; 158 }; 159 // 160 // OK, lets do it now: 161 // 162 System.err.println( 163 "#\n# Terminator: prescheduled program termination.\n#"); 164 System.exit(exitStatus); // terminator to all threads 165 } 166 }; 167 168 terminator.setPriority(Thread.MAX_PRIORITY); 169 terminator.setDaemon(true); 170 terminator.start(); 171 } 172 } |