addComponent(new Button("Cancel running future",
new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
log.clear();
final ReentrantLock interruptLock = new ReentrantLock();
final Future<Void> future = access(new Runnable() {
@Override
public void run() {
log("Waiting for thread to start");
while (!interruptLock.isLocked()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
log("Premature interruption");
throw new RuntimeException(e);
}
}
log("Thread started, waiting for interruption");
try {
interruptLock.lockInterruptibly();
} catch (InterruptedException e) {
log("I was interrupted");
}
}
});
new Thread() {
@Override
public void run() {
interruptLock.lock();
// Wait until UI thread has started waiting for
// the lock
while (!interruptLock.hasQueuedThreads()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}