I have a class that extends Thread. This thread when running spends most of it\'s time sleeping, it will perform a check, if true perform a simple action, then sleep for 1/2
Add this to your thread:
public AtomicBoolean waitLonger = new AtomicBoolean ();
public Object lock = new Object ();
In run()
:
synchronized (lock) {
if (waitLonger.get ()) {
lock.wait ();
}
}
In the other thread:
synchronized (lock) {
try {
sleeper.waitLonger.set(true);
...
lock.notify();
sleeper.waitLonger.set(false);
}
This way, you can make the sleeper wait until the other work has completed.