...
Code Block | ||
---|---|---|
| ||
public class Waiter { private Thread thread; private boolean flag; private final Object lock = new Object(); public void doSomething() { thread = new Thread(new Runnable() { @Override public void run() { synchronized(lock) { while (!flag) { try { lock.wait(); // ... } catch (InterruptedException e) { // Forward to handler } } } } }); thread.start(); } public boolean stop() { if (thread != null) { if (thread.getState() == Thread.State.WAITING) { flagsynchronized = true; (lock) { synchronizedflag (lock) {= true; lock.notifyAll(); } return true; } } return false; } } |
...