// This file contains excerpts from the textbook Concrete // Abstractions: An Introduction to Computer Science Using Scheme, by // Max Hailperin, Barbara Kaiser, and Karl Knight, Copyright (c) 1998 // by the authors. Full text is available for free at // http://www.gustavus.edu/+max/concrete-abstractions.html public class PoltergeistThread extends Thread { private Puzzle puz; private boolean enabled; public PoltergeistThread(Puzzle p){ puz = p; } public void run(){ try{ while(true){ Thread.sleep(1000); // 1000 milliseconds = 1 second waitUntilEnabled(); puz.pushRandomTile(); } } catch(InterruptedException e){ // If the thread is forcibly interrupted while sleeping, // an exception gets thrown that is caught here. However, // we can't really do anything, except stop running. } } // Warning: these two routines don't work, see below (in text) for why. private void waitUntilEnabled() throws InterruptedException { while(!enabled){ wait(); } } public void enable(){ enabled = true; notify(); } public void disable(){ enabled = false; } }