I saw a question from somebody who is not able/willing to change a long-running method to return a Future<Something>
instead of just <Something>
. Unfortunately, the method gets called also from the AWT-Thread with the known consequences. I've told him, he's doing nonsense, but there might be a possibility. He could start a SwingWorker (or whatever) from inside his method and process the AWT-Queue while wa开发者_开发百科iting till it finishes.
I mean something like
public Something aLongRunningMethodCalledFromTheAwtThread() {
FutureTask<Something> future = new FutureTask<Something>(...);
EventQueue eventQueue = Toolkit.getEventQueue();
while (true) {
if (future.isDone()) return future.get();
AWTEvent event = eventQueue.getCurrentEvent();
if (event==null) {
waitForAWhile();
} else {
eventQueue.pop(); // <---- is protected
process(event); // <---- BUT HOW???
}
}
}
AFAIK, such things get done in other frameworks, I wonder if this is possible in Swing/AWT too?
There's no point in implementing an upside solution when a right-side-up solution already exists. The method needs to be executed as a separate thread. Period.
精彩评论