既然您不介意在服务器上继续过时的 EJB 调用,为什么不让调用线程“自然地”终止,而是丢弃结果,因为它的调用已被另一个线程取代?我没有时间提供示例实现,但您可能会发现您对Future
s 和相关的 Java 并发类有所了解。
编辑
除此之外,您可能会发现类似这样的方法可以解决问题,但对我来说感觉很老套,我相信还有更优雅的解决方案。
在调用线程上(可能是按钮的 onclick 方法):
AsynchronousResultManager.registerRequest("UNIQUE_IDENTIFIER", runnableExecuteRequest);
registerRequest
会做类似的事情:
registerClick(String id, Runnable execution) {
AtomicReference ref = executions.get(id); //executions is a Map<String, AtomicReference> created as a a computing map by Guava MapMaker
execution.setReference(ref); //so that the Runnable has a reference to it later
ref.set(execution); //this will overwrite an existing reference to a previous invocation.
//here you need to actually kick off your thread in whatever way works best for you
}
执行请求的runnable
将是以下的子类:
public abstract class RequestRunnable implements Runnable {
private AtomicReference ref;
public void run() {
doRunInternal(); //actually go off and do the request to the J2EE server
if (this == ref.get()) { //ie if the current runnable is the same as in the reference, we can proceed to actually dispatch the result
dispatchResult(); //this method would do something like add a runnable to the SwingWorkerThread
}
}
protected abstract void doRunInternal();
protected abstract void dispatchResult();
public void setReference(AtomicReference ref) {
this.ref = ref;
}
}
这可能会崩溃并烧毁,但希望它可以为您指明一条询问线……