2 * Copyright (c) 2005, the JUNG Project and the Regents of the University of
3 * California All rights reserved.
5 * This software is open-source under the BSD license; see either "license.txt"
6 * or http://jung.sourceforge.net/license.txt for a description.
10 package edu.uci.ics.jung.algorithms.layout.util;
12 import edu.uci.ics.jung.algorithms.util.IterativeContext;
16 * Implementation of a relaxer thread for layouts.
17 * Extracted from the {@code VisualizationModel} in previous
20 * @author Tom Nelson - tomnelson@dev.java.net
23 public class VisRunner implements Relaxer, Runnable {
25 protected boolean running;
26 protected IterativeContext process;
27 protected boolean stop;
28 protected boolean manualSuspend;
29 protected Thread thread;
32 * how long the relaxer thread pauses between iteration loops.
34 protected long sleepTime = 100L;
38 * Creates an instance for the specified process.
40 public VisRunner(IterativeContext process) {
41 this.process = process;
45 * @return the relaxerThreadSleepTime
47 public long getSleepTime() {
52 * @param sleepTime the sleep time to set for this thread
54 public void setSleepTime(long sleepTime) {
55 this.sleepTime = sleepTime;
58 public void prerelax() {
60 long timeNow = System.currentTimeMillis();
61 while (System.currentTimeMillis() - timeNow < 500 && !process.done()) {
64 manualSuspend = false;
72 // in case its running
75 thread = new Thread(this);
76 thread.setPriority(Thread.MIN_PRIORITY);
81 * Used for synchronization.
83 public Object pauseObject = new String("PAUSE OBJECT");
85 public void resume() {
86 manualSuspend = false;
87 if(running == false) {
91 synchronized(pauseObject) {
92 pauseObject.notifyAll();
97 public synchronized void stop() {
99 manualSuspend = false;
101 // interrupt the relaxer, in case it is paused or sleeping
102 // this should ensure that visRunnerIsRunning gets set to false
103 try { thread.interrupt(); }
104 catch(Exception ex) {
105 // the applet security manager may have prevented this.
106 // just sleep for a second to let the thread stop on its own
107 try { Thread.sleep(1000); }
108 catch(InterruptedException ie) {} // ignore
110 synchronized (pauseObject) {
111 pauseObject.notifyAll();
119 while (!process.done() && !stop) {
120 synchronized (pauseObject) {
121 while (manualSuspend && !stop) {
124 } catch (InterruptedException e) {
135 Thread.sleep(sleepTime);
136 } catch (InterruptedException ie) {