调用IJob.execute()或FutureTask.run()会阻塞当前线程,需要调度IJob或未来任务。
调度 FutureTask 是最好的选择,消费者可以调用 FutureTask.get() 并等待结果(即使你调用 IJob.cancel( ))。
我做了一个模拟 IJob 和 IResult 的演示,它使用普通线程进行调度,在生产中你应该有一个像以前一样的 ExecutorService发布示例。
如你所见,主线程可以检查调用FutureTask.isDone()的状态,基本上你是在检查结果是否已经设置。设置的结果意味着IJob的线程已经结束。
您几乎可以随时调用 IJob.cancel() 来完成 FutureTask 中的包装 IJob厘米。
模拟工作:
public class MockJob implements IJob {
private boolean cancelled;
public MockJob() {
}
@Override
public IResult execute() {
int count = 0;
while (!cancelled) {
try {
count++;
System.out.println("Mock Job Thread: count = " + count);
if (count >= 10) {
break;
}
Thread.sleep(1000);
} catch (InterruptedException e) {
cancelled = true;
}
}
return new MockResult(cancelled, count);
}
@Override
public void cancel() {
cancelled = true;
}
}
模拟结果:
public class MockResult implements IResult {
private boolean cancelled;
private int result;
public MockResult(boolean cancelled, int result) {
this.cancelled = cancelled;
this.result = result;
}
public boolean isCancelled() {
return cancelled;
}
public int getResult() {
return result;
}
}
主类:
public class Main {
public static void main(String[] args) throws InterruptedException {
// Job
IJob mockJob = new MockJob();
// Async task
FutureTask<IResult> asyncTask = new FutureTask<>(mockJob::execute);
Thread mockJobThread = new Thread(asyncTask);
// Show result
Thread showResultThread = new Thread(() -> {
try {
IResult result = asyncTask.get();
MockResult mockResult = (MockResult) result;
Thread thread = Thread.currentThread();
System.out.println(String.format("%s: isCancelled = %s, result = %d",
thread.getName(),
mockResult.isCancelled(),
mockResult.getResult()
));
} catch (InterruptedException | ExecutionException ex) {
// NO-OP
}
});
// Check status
Thread monitorThread = new Thread(() -> {
try {
while (!asyncTask.isDone()) {
Thread thread = Thread.currentThread();
System.out.println(String.format("%s: asyncTask.isDone = %s",
thread.getName(),
asyncTask.isDone()
));
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
// NO-OP
}
Thread thread = Thread.currentThread();
System.out.println(String.format("%s: asyncTask.isDone = %s",
thread.getName(),
asyncTask.isDone()
));
});
// Async cancel
Thread cancelThread = new Thread(() -> {
try {
// Play with this Thread.sleep, set to 15000
Thread.sleep(5000);
if (!asyncTask.isDone()) {
Thread thread = Thread.currentThread();
System.out.println(String.format("%s: job.cancel()",
thread.getName()
));
mockJob.cancel();
}
} catch (InterruptedException ex) {
// NO-OP
}
});
monitorThread.start();
showResultThread.start();
cancelThread.setDaemon(true);
cancelThread.start();
mockJobThread.start();
}
}
输出(Thread.sleep(5000)):
Thread-2: asyncTask.isDone = false
Thread-0: count = 1
Thread-2: asyncTask.isDone = false
Thread-0: count = 2
Thread-2: asyncTask.isDone = false
Thread-0: count = 3
Thread-2: asyncTask.isDone = false
Thread-0: count = 4
Thread-2: asyncTask.isDone = false
Thread-0: count = 5
Thread-3: job.cancel()
Thread-2: asyncTask.isDone = false
Thread-1: isCancelled = true, result = 5
Thread-2: asyncTask.isDone = true
输出(Thread.sleep(15000)):
Thread-2: asyncTask.isDone = false
Thread-0: count = 1
Thread-2: asyncTask.isDone = false
Thread-0: count = 2
Thread-2: asyncTask.isDone = false
Thread-0: count = 3
Thread-2: asyncTask.isDone = false
Thread-0: count = 4
Thread-2: asyncTask.isDone = false
Thread-0: count = 5
Thread-2: asyncTask.isDone = false
Thread-0: count = 6
Thread-2: asyncTask.isDone = false
Thread-0: count = 7
Thread-2: asyncTask.isDone = false
Thread-0: count = 8
Thread-2: asyncTask.isDone = false
Thread-0: count = 9
Thread-2: asyncTask.isDone = false
Thread-0: count = 10
Thread-1: isCancelled = false, result = 10
Thread-2: asyncTask.isDone = true