开发者

Is there a way to process the AWT-Queue from own method?

开发者 https://www.devze.com 2023-02-19 07:11 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消