From b8eccd977caae279a6ca4de5de52f1506ca736ff Mon Sep 17 00:00:00 2001 From: Jochen Keil Date: Sat, 3 Jul 2010 09:55:59 +0200 Subject: [PATCH] apply the observer pattern for the gui to get notifications from AntView the update method does the following: it removes the applet from the list of observers in the Environment (which updates the graph etc.) then it sets antView to null since it got disposed; we check somewhere else for it being null; if the Environment thread is not null and hasn't terminated yet we send it an interrupt so it can return gracefully. when this is done we set antThread to null so we can check for it later finally we need to reset the button text and after everything is done we run the gc since it might take a view seconds before a user wants to start a new computation --- src/aco/TspAcoGui.java | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/aco/TspAcoGui.java b/src/aco/TspAcoGui.java index fae428d..f42c2ca 100644 --- a/src/aco/TspAcoGui.java +++ b/src/aco/TspAcoGui.java @@ -8,15 +8,21 @@ import java.io.*; import java.util.ArrayList; import java.util.Collections; -import aco.environment.*; +import java.util.Observer; +import java.util.Observable; + +import aco.antview.*; +import aco.mediator.*; public class TspAcoGui extends JFrame - implements PopupMenuListener, ItemListener, ActionListener { + implements PopupMenuListener, ItemListener, ActionListener, Observer { static final long serialVersionUID = 100000000L; protected AntView antView = null; + protected ACOMediator acom = null; + protected Thread antThread = null; protected final String tspdir = "../src/aco/tspproblems/"; String filename;// = "rat99.tsp"; @@ -332,11 +338,30 @@ public class TspAcoGui antThread = EnvironmentFactory.acs(filename, beta, q, roh, cl, ants, runs); break; - } return antThread; } + public void update(Observable o, Object arg) { + acom.getEnvironment().deleteObserver(antView); + antView = null; + + if (antThread != null) { + antThread.interrupt(); + try { + antThread.join(); + } catch (InterruptedException ie) { + System.err.println(ie); + return; + } + } + + antThread = null; + startButton.setText("Run selected Algorithm"); + System.gc(); + + } + public static void main(String[] args) { new TspAcoGui(); } -- 2.11.4.GIT