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