/** * Return the value created by the <code>construct</code> method. * Returns null if either the constructing thread or the current * thread was interrupted before a value was produced. * * @return the value created by the <code>construct</code> method */ public Object get() { while (true) { Thread t = threadVar.get(); if (t == null) { return getValue(); } try { t.join(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // propagate return null; } } }
/** * Start a thread that will call the <code>construct</code> method * and then exit. */ public SwingWorker() { new Runnable() { public void run() { finished(); } }; Runnable doConstruct = () -> { try { setValue(construct()); } catch ( Exception e ) { Log.error( e ); } finally { threadVar.clear(); } SwingUtilities.invokeLater( () -> finished() ); }; Thread t = new Thread(doConstruct); threadVar = new ThreadVar(t); }
/** * Start the worker thread. */ public void start() { Thread t = threadVar.get(); if (t != null) { t.start(); } } }