Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're using <code>Swing</code>, the <code>SwingWorker</code> provides capabilities for this, and you don't have to deal with the thread pool yourself.</p> <p>Fire off a <code>SwingWorker</code> for each request. If a new request comes in and the worker is not done, you can <code>cancel()</code> it, and just start a new <code>SwingWorker</code>. Regarding what the other poster said, I don't think <code>publish()</code> and <code>process()</code> are what you are looking for (although they are also very useful), since they are meant for a case where the worker might fire off events faster than the GUI can process it.</p> <pre><code>ThingyWorker worker; public void actionPerformed(ActionEvent e) { if( worker != null ) worker.cancel(); worker = new ThingyWorker(); worker.execute(); } class ThingyWorker extends SwingWorker&lt;YOURCLASS, Object&gt; { @Override protected YOURCLASS doInBackground() throws Exception { return doSomeComputation(); // Should be interruptible } @Override protected void done() { worker = null; // Reset the reference to worker YOURCLASS data; try { data = get(); } catch (Exception e) { // May be InterruptedException or ExecutionException e.printStackTrace(); return; } // Do something with data } } </code></pre> <p>Both the action and the <code>done()</code> method are executed on the same thread, so they can effectively check the reference to whether there is an existing worker.</p> <p>Note that effectively this is doing the same thing that allows a GUI to cancel an existing operation, except the cancel is done automatically when a new request is fired.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload