| [56de6b39] | 1 | class Monitor { | 
|---|
|  | 2 | public static volatile Boolean go = false; | 
|---|
| [be53b87] | 3 | public static volatile Boolean next = false; | 
|---|
| [56de6b39] | 4 | } | 
|---|
| [6aa537a4] | 5 |  | 
|---|
| [56de6b39] | 6 | class Signaller extends Thread { | 
|---|
|  | 7 | Monitor m; | 
|---|
|  | 8 | Signaller(Monitor m) { | 
|---|
|  | 9 | this.m = m; | 
|---|
|  | 10 | } | 
|---|
| [6aa537a4] | 11 |  | 
|---|
| [56de6b39] | 12 | public void run() { | 
|---|
|  | 13 | Monitor.go = true; | 
|---|
|  | 14 | while( Monitor.go ) { | 
|---|
|  | 15 | synchronized(this.m) { | 
|---|
| [be53b87] | 16 | Monitor.next = false; | 
|---|
| [56de6b39] | 17 | this.m.notify(); | 
|---|
| [6aa537a4] | 18 | } | 
|---|
| [be53b87] | 19 | while( ! Monitor.next && Monitor.go );  // spin until woken | 
|---|
| [6aa537a4] | 20 | } | 
|---|
|  | 21 | } | 
|---|
| [56de6b39] | 22 | } | 
|---|
| [6aa537a4] | 23 |  | 
|---|
| [56de6b39] | 24 | public class JavaThread { | 
|---|
| [be53b87] | 25 | // Simplistic low-quality Marsaglia Shift-XOR pseudo-random number generator. | 
|---|
| [2a658e9] | 26 | // Bijective | 
|---|
| [be53b87] | 27 | // Cycle length for non-zero values is 4G-1. | 
|---|
|  | 28 | // 0 is absorbing and should be avoided -- fixed point. | 
|---|
|  | 29 | // The returned value is typically masked to produce a positive value. | 
|---|
| [2a658e9] | 30 | static volatile int Ticket = 0 ; | 
|---|
| [be53b87] | 31 |  | 
|---|
|  | 32 | private static int nextRandom (int x) { | 
|---|
| [2a658e9] | 33 | if (x == 0) { | 
|---|
| [be53b87] | 34 | // reseed the PRNG | 
|---|
| [2a658e9] | 35 | // Ticket is accessed infrequently and does not constitute a coherence hot-spot. | 
|---|
|  | 36 | // Note that we use a non-atomic racy increment -- the race is rare and benign. | 
|---|
|  | 37 | // If the race is a concern switch to an AtomicInteger. | 
|---|
|  | 38 | // In addition accesses to the RW volatile global "Ticket"  variable are not | 
|---|
|  | 39 | // (readily) predictable at compile-time so the JIT will not be able to elide | 
|---|
|  | 40 | // nextRandom() invocations. | 
|---|
|  | 41 | x = ++Ticket ; | 
|---|
|  | 42 | if (x == 0) x = 1 ; | 
|---|
| [be53b87] | 43 | } | 
|---|
|  | 44 | x ^= x << 6; | 
|---|
|  | 45 | x ^= x >>> 21; | 
|---|
|  | 46 | x ^= x << 7; | 
|---|
| [2a658e9] | 47 | return x ; | 
|---|
| [be53b87] | 48 | } | 
|---|
|  | 49 | static int x = 2; | 
|---|
|  | 50 |  | 
|---|
| [2a658e9] | 51 | static private long times = Long.parseLong("1000000"); | 
|---|
| [be53b87] | 52 |  | 
|---|
|  | 53 | public static void helper( Monitor m ) throws InterruptedException { | 
|---|
| [2a658e9] | 54 | for(long i = 1; i <= times; i += 1) { | 
|---|
| [be53b87] | 55 | m.wait();               // relase monitor lock | 
|---|
|  | 56 | m.next = true; | 
|---|
|  | 57 | } | 
|---|
|  | 58 | } | 
|---|
|  | 59 | public static void InnerMain() throws InterruptedException { | 
|---|
| [56de6b39] | 60 | Monitor m = new Monitor(); | 
|---|
|  | 61 | long start, end; | 
|---|
|  | 62 | Signaller s = new Signaller(m); | 
|---|
|  | 63 | synchronized(m) { | 
|---|
|  | 64 | s.start(); | 
|---|
| [26fd986] | 65 | while( ! Monitor.go ) { // waiter must start first | 
|---|
| [56de6b39] | 66 | Thread.yield(); | 
|---|
|  | 67 | } | 
|---|
|  | 68 | start = System.nanoTime(); | 
|---|
| [be53b87] | 69 | helper( m ); | 
|---|
| [56de6b39] | 70 | end = System.nanoTime(); | 
|---|
| [6aa537a4] | 71 | } | 
|---|
| [56de6b39] | 72 | Monitor.go = false; | 
|---|
| [6aa537a4] | 73 | s.join(); | 
|---|
| [b4107c8] | 74 | System.out.println( (end - start) / times); | 
|---|
| [6aa537a4] | 75 | } | 
|---|
| [be53b87] | 76 | public static void main(String[] args) throws InterruptedException { | 
|---|
| [2c3562d] | 77 | if ( args.length > 1 ) System.exit( 1 ); | 
|---|
|  | 78 | if ( args.length == 1 ) { times = Long.parseLong(args[0]); } | 
|---|
| [b4107c8] | 79 |  | 
|---|
| [2a658e9] | 80 | for (int n = Integer.parseInt("5"); --n >= 0 ; ) { | 
|---|
| [be53b87] | 81 | InnerMain(); | 
|---|
|  | 82 | Thread.sleep(2000);     // 2 seconds | 
|---|
|  | 83 | x = nextRandom(x); | 
|---|
|  | 84 | } | 
|---|
|  | 85 | if ( x == 0 ) System.out.println(x); | 
|---|
|  | 86 | } | 
|---|
|  | 87 | } | 
|---|
| [b4107c8] | 88 |  | 
|---|
|  | 89 | // Local Variables: // | 
|---|
|  | 90 | // tab-width: 4 // | 
|---|
|  | 91 | // End: // | 
|---|