title: How to properly extend FutureTask
body: <p>While coding a computation-heavy application, I tried to make use of the SwingWorker class to spread the load to multiple CPU cores. However, behaviour of this class proved to be somewhat strange: only one core seemed to be utilized. </p>
  
  <p>When searching the internet, I found an excellent answer on this web (see <a href="https://stackoverflow.com/questions/7548253/swingworker-instances-not-running-concurrently/7548506#7548506">Swingworker instances not running concurrently</a>, answer by user268396) which -- in addition to the cause of the problem -- also mentions a possible solution:</p>
  
  <blockquote>
    <p>What you can do to get around this is use an ExecutorService and post
    FutureTasks on it. These will provide 99% of the SwingWorker API
    (SwingWorker is a FutureTask derivative), all you have to do is set up
    your Executor properly.</p>
  </blockquote>
  
  <p>Being a Java beginner, I am not entirely sure how to do this properly. Not only that I need to pass some initial data to the FutureTask objects, I also need to get the results back similarly as with SwingWorker. Any example code would therefore be much appreciated.</p>
  
  <p>nvx</p>
  
  <p>==================== EDIT ====================</p>
  
  <p>After implementing the simple yet elegant solution mentioned in <a href="https://stackoverflow.com/questions/1468598/futuretask-that-implements-callable">FutureTask that implements Callable</a>, another issue has come up. If I use an <code>ExecutorService</code> to create individual threads, how do I execute specific code after a thread finished running?</p>
  
  <p>I tried to override done() of the <code>FutureTask</code> object (see the code below) but I guess that the "show results" bit (or any GUI related stuff for that matter) should be done in the application's event dispatch thread (EDT). Therefore: how do I submit the runnable to the EDT?</p>
  
  <pre><code>package multicoretest;
  
  import java.util.concurrent.*;
  
  public class MultiCoreTest {
  
      static int coresToBeUsed = 4;
      static Future[] futures = new Future[coresToBeUsed];
  
      public static void main(String[] args) {
          ExecutorService execSvc = Executors.newFixedThreadPool(coresToBeUsed);
          for (int i = 0; i &lt; coresToBeUsed; i++) {
              futures[i] = execSvc.submit(new Worker(i));
          }
          execSvc.shutdown();
  
          // I do not want to block the thread (so that users can
          // e.g. terminate the computation via GUI)
          //execSvc.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
  
      }
  
      static class Worker implements Callable&lt;String&gt; {
  
          private final FutureTask&lt;String&gt; futureTask;
          private final int workerIdx;
  
          public Worker(int idx) {
              workerIdx = idx;
              futureTask = new FutureTask&lt;String&gt;(this) {
                  @Override
                  protected void done() {
                      Runnable r = new Runnable() {
                          @Override
                          public void run() {
                              showResults(workerIdx);
                          }
                      };
  
                      r.run(); // Does not work =&gt; how do I submit the runnable
                               // to the application's event dispatch thread?
  
                  }
              };
          }
  
          @Override
          public String call() throws Exception {
              String s = "";
              for (int i = 0; i &lt; 2e4; i++) {
                  s += String.valueOf(i) + " ";
              }
              return s;
          }
  
          final String get() throws InterruptedException, ExecutionException {
              return futureTask.get();
          }
  
          void showResults(int idx) {
              try {
                  System.out.println("Worker " + idx + ":" +
                          (String)futures[idx].get());
              } catch (Exception e) {
                  System.err.println(e.getMessage());
              }
          }
      }
  }
  </code></pre>