Reputation: 5398
I am trying to run a swing worker using the Executor Service and something strange is happening. I am currently using the method newSingleThreadExecutor();
as well as doing the following;
Future<?> f = execService.submit(swingWorker);
JOptionPane.showMessageDialog(null, "Created Future");
f.get(120, TimeUnit.SECONDS);
What happens is that my swing worker code runs perfectly so long as I don't close the dialog message box. If I close the dialog box then my application just completely freezes. I believe the thread is getting blocked but am unsure for what reason.
If I run the swingworker on its own without the help of a execService it works perfectly, but of course the timeout feature is not available which is the whole purpose of this. Should I just use timer instead?
Thanks
Upvotes: 0
Views: 884
Reputation: 691735
Future.get()
is a blocking call. It waits until the task has completed, or until the timeout has elapsed. So, by calling it in the EDT, you're doing exactly what you try to avoid by using a SwingWorker.
Upvotes: 1
Reputation: 40256
If it looks something like:
public void closeDialog(){
SwingUtitilies.invokeLater(new Runnable(){
public void run(){
Future<?> f = execService.submit(swingWorker);
JOptionPane.showMessageDialog(null, "Created Future");
f.get(120, TimeUnit.SECONDS);
}
});
}
You are blocking (suspending) the EDT. f.get()
will block until the Future's processing completes.
Upvotes: 2
Reputation: 7302
I'm not a swing expert but in Java Concurrency in Practice it is mentioned that swing uses it's own thread for UI management, and you shall not interfere with it. May be that's the problem.
Upvotes: 0