From - Sat Mar  9 16:19:41 1996
Path: news.isaac.cs.berkeley.edu!not-for-mail
From: strick -- henry strickland <strix@versant.com>
Newsgroups: isaac.lists.coderpunks
Subject: java spinner
Date: 6 Mar 1996 13:53:31 -0800
Organization: ISAAC Group, UC Berkeley
Lines: 66
Sender: bin@abraham.cs.berkeley.edu
Approved: mail2news@news.isaac.cs.berkeley.edu
Distribution: isaac
Message-ID: <9603062024.AA25642@vp.versant.com>
NNTP-Posting-Host: abraham.cs.berkeley.edu
Precedence: bulk

// 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.
