Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions digipres-tika/src/main/java/uk/bl/wa/tika/CallableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
import java.util.concurrent.TimeoutException;

/**
* @author Andrew Jackson <Andrew.Jackson@bl.uk>
*
* Demonstrates timeout-safe task execution with cooperative interruption.
*
* @author Andrew Jackson
*/
public class CallableTest {
public static void main(String[] args) throws Exception {
Expand All @@ -43,20 +44,23 @@ public static void main(String[] args) throws Exception {

try {
System.out.println("Started..");
System.out.println(future.get(3, TimeUnit.SECONDS));
System.out.println(future.get(3, TimeUnit.SECONDS)); // Wait up to 3s
System.out.println("Finished!");
} catch (TimeoutException e) {
System.out.println("Terminated!");
System.out.println("Terminated due to timeout!");
future.cancel(true); // Cooperative cancel on timeout
} finally {
executor.shutdownNow();
}

executor.shutdownNow();
}
}

class Task implements Callable<String> {
@Override
public String call() throws Exception {
Thread.sleep(4000); // Just to demo a long running task of 4 seconds.
return "Ready!";
while (!Thread.currentThread().isInterrupted()) {
Thread.sleep(100); // Simulate ongoing work
}
return "Cancelled!";
}
}
Loading