Since this is with the Swing GUI, consider using a SwingWorker object that creates a background thread (all code is executed in the doInBackground method) and then can return the final result and / or intermediate results. Information on how to use it is well described in textbooks:
Concurrency in Swing
SwingWorkers has support for changing properties, and thus allows listeners to observe their state (like SwingWorker.StateValue) using the PropertyChangeListener. This is one of the ways that your program can determine that the thread has completed its processing, get the return result, and go from there.
In an unrelated note, this is not in your production code, is it ?:
catch (Exception e) {}
If this is the case, you will most likely want to fix it, because ignored exceptions can bite you in the tail for a long time.
eg.
if (turn == white) { try { final SwingWorker<Move, Void> mySwingWorker = new SwingWorker<Move, Void>() { @Override protected Move doInBackground() throws Exception { Engine e = new Engine(); // Engine is implemented by runnable e.start(); Move m = e.getBestMove(board); return m; } }; mySwingWorker.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if (StateValue.DONE == mySwingWorker.getState()) { try { Move m = mySwingWorker.get(); // TODO: insert code to run on the EDT after move determined } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } } }); mySwingWorker.execute(); } catch (Exception e) { e.printStackTrace(); } }
source share