Changeset be53b87 for benchmark/schedint
- Timestamp:
- Jul 29, 2019, 1:46:24 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 5453237
- Parents:
- 033ff37
- git-author:
- Peter A. Buhr <pabuhr@…> (07/29/19 12:40:16)
- git-committer:
- Peter A. Buhr <pabuhr@…> (07/29/19 13:46:24)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
benchmark/schedint/JavaThread.java
r033ff37 rbe53b87 1 1 class Monitor { 2 2 public static volatile Boolean go = false; 3 public static volatile Boolean next = false; 3 4 } 4 5 … … 13 14 while( Monitor.go ) { 14 15 synchronized(this.m) { 16 Monitor.next = false; 15 17 this.m.notify(); 16 18 } 19 while( ! Monitor.next && Monitor.go ); // spin until woken 17 20 } 18 21 } … … 20 23 21 24 public class JavaThread { 22 public static void main(String[] args) throws InterruptedException { 23 int NoOfTimes = 50000; 25 // Simplistic low-quality Marsaglia Shift-XOR pseudo-random number generator. 26 // Bijective 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. 30 static volatile int Ticket = 0 ; 31 32 private static int nextRandom (int x) { 33 if (x == 0) { 34 // reseed the PRNG 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 ; 43 } 44 x ^= x << 6; 45 x ^= x >>> 21; 46 x ^= x << 7; 47 return x ; 48 } 49 static int x = 2; 50 51 static private final int NoOfTimes = Integer.parseInt("1000000") ; 52 53 public static void helper( Monitor m ) throws InterruptedException { 54 for(int i = 1; i <= NoOfTimes; i += 1) { 55 m.wait(); // relase monitor lock 56 m.next = true; 57 } 58 } 59 public static void InnerMain() throws InterruptedException { 24 60 Monitor m = new Monitor(); 25 61 long start, end; … … 31 67 } 32 68 start = System.nanoTime(); 33 for(int i = 1; i <= NoOfTimes; i += 1) { 34 m.wait(); 35 } 69 helper( m ); 36 70 end = System.nanoTime(); 37 71 } … … 40 74 System.out.println( (end - start) / NoOfTimes); 41 75 } 76 public static void main(String[] args) throws InterruptedException { 77 for (int n = Integer.parseInt("5"); --n >= 0 ; ) { 78 InnerMain(); 79 Thread.sleep(2000); // 2 seconds 80 x = nextRandom(x); 81 } 82 if ( x == 0 ) System.out.println(x); 83 } 42 84 }
Note: See TracChangeset
for help on using the changeset viewer.