Subject: java spinner

From: strick -- henry strickland <[email-masked]>
Date: 6 Mar 1996 13:53:31 -0800

// Thought i should share this.  can you simplify?  --strick
//
//  (A) 1996, Henry Strickland,  no rites rzrvd.

class SpinStop extends Throwable {
        SpinStop() {}
}
 
class SpinSlave extends Thread {
        long millis;
        Thread parent;
        SpinSlave(long millis, Thread parent) {
                this.millis= millis;
                this.parent= parent;
        }
 
        public void run() {
                try { 
                        Thread.sleep(millis);
                        parent.stop(new SpinStop());
                        stop();
                } catch (InterruptedException ex) {
                        parent.stop(ex);
                }
        }
}
 
class SpinMaster extends Thread {
        long millis;
        long counter;
 
        SpinMaster(long millis) {
                this.millis= millis;
                this.counter= 0;
        }
 
        public void run() {
 
                try {
 
                        Thread t= new SpinSlave(millis, this);
                        t.start();
 
                        while (true) {
                                counter ++;
                                Thread.yield();
                        }
 
                } catch (SpinStop s) {
                }
        }
}
 
public class Spinner {
 
        public static long spin(long millis) throws InterruptedException {
 
                SpinMaster t= new SpinMaster(millis);
                t.start();
                t.join();
                return t.counter;
        }
}
 
// end.