Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

backgrounds tasks by swingworkers become sequential

I am using SwingWorkers to make my GUI responsive. But I can not understand the following: I have a remote method I want to call from the GUI as the user presses a button. I have inside the action of the button (short for presentation of problem):

//in action of button
SwingWorker worker = new SwingWorker<boolean,Void>(){
   @Override
   public boolean doInBackground(){
      return call_remote_method_here();
   }

};
worker.execute();
//some other code

My problem is that although the gui seems responsive the actual calls to the remote method are sequential and not concurrent . I noticed it in network traces, and thought that the method was blocking. But then I saw in the traces that the SwingWorkers seem to be running sequential. From

log 2010-09-06 16:58:22,962 [SwingWorker-pool-4-thread-1] DEBUG -->First remote method call

I get the response and then the second swingworker runs

2010-09-06 16:58:23,227 [SwingWorker-pool-4-thread-1] DEBUG --> Second remote method call

Why is this happening? I thought that it was random interleaving but I noticed that the 2 instances have the same name? Are the swing workers pooled? Is this a random behavior? Shouln't I see 2 concurrent calls? If they are being reused, is there anything I can do to have concurrent calls and not parallism?

Thanks

like image 769
Cratylus Avatar asked Dec 22 '22 23:12

Cratylus


1 Answers

The problem is, that all Swingworkers run on the same Background-Thread. When you want them to run concurrent you could add them to an Executor Service like this for example:

Executor executor = Executors.newCachedThreadPool();
executor.execute(yourSwingWorker);

Source: Oracle Forums

like image 168
crusam Avatar answered Jan 07 '23 01:01

crusam