1 11 package org.eclipse.jdt.internal.debug.core.model; 12 13 14 20 public class Timer { 21 22 25 private ITimeoutListener fListener; 26 27 30 private int fTimeout; 31 32 35 private boolean fAlive = true; 36 37 41 private boolean fStarted = false; 42 43 46 private Thread fThread; 47 48 51 public Timer() { 52 setTimeout(Integer.MAX_VALUE); 53 Runnable r = new Runnable () { 54 public void run() { 55 while (isAlive()) { 56 boolean interrupted = false; 57 try { 58 Thread.sleep(getTimeout()); 59 } catch (InterruptedException e) { 60 interrupted = true; 61 } 62 if (!interrupted) { 63 if (getListener() != null) { 64 setStarted(false); 65 setTimeout(Integer.MAX_VALUE); 66 getListener().timeout(); 67 setListener(null); 68 } 69 } 70 } 71 } 72 }; 73 setThread(new Thread (r, "Evaluation Timer")); getThread().setDaemon(true); 75 getThread().start(); 76 } 77 78 89 public void start(ITimeoutListener listener, int ms) { 90 if (isStarted()) { 91 throw new IllegalStateException (JDIDebugModelMessages.Timer_Timer_cannot_be_started_more_than_once_1); 92 } 93 setListener(listener); 94 setTimeout(ms); 95 setStarted(true); 96 getThread().interrupt(); 97 } 98 99 103 public void stop() { 104 if (isAlive()) { 105 setStarted(false); 106 setTimeout(Integer.MAX_VALUE); 107 getThread().interrupt(); 108 } 109 } 110 111 114 public void dispose() { 115 if (isAlive()) { 116 setAlive(false); 117 getThread().interrupt(); 118 setThread(null); 119 } 120 } 121 122 127 private boolean isAlive() { 128 return fAlive; 129 } 130 131 140 private void setAlive(boolean alive) { 141 fAlive = alive; 142 } 143 144 149 protected ITimeoutListener getListener() { 150 return fListener; 151 } 152 153 159 private void setListener(ITimeoutListener listener) { 160 fListener = listener; 161 } 162 163 170 public boolean isStarted() { 171 return fStarted; 172 } 173 174 181 private void setStarted(boolean started) { 182 fStarted = started; 183 } 184 185 190 private Thread getThread() { 191 return fThread; 192 } 193 194 200 private void setThread(Thread thread) { 201 fThread = thread; 202 } 203 204 210 protected int getTimeout() { 211 return fTimeout; 212 } 213 214 220 private void setTimeout(int timeout) { 221 fTimeout = timeout; 222 } 223 } 224 | Popular Tags |