ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/FinalLongTest.java
Revision: 1.8
Committed: Thu Jan 15 18:34:19 2015 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +0 -1 lines
Log Message:
delete extraneous blank lines

File Contents

# User Rev Content
1 dl 1.2 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.6 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.2 */
6 dl 1.1
7     public class FinalLongTest {
8     static int npairs = 2;
9     static int iters = 10000000;
10 jsr166 1.7 static int LEN = 2;
11 dl 1.1 static final Long[] nums = new Long[LEN];
12     static volatile boolean done;
13     static volatile long total;
14     static Long n0 = new Long(21);
15     static Long n1 = new Long(22);
16     static Long n2 = new Long(23);
17     static Long n3 = new Long(23);
18 jsr166 1.3
19 dl 1.1 public static void main(String[] args) {
20     for (int i = 0; i < LEN; ++i)
21     nums[i] = new Long(i+1);
22     Thread[] ps = new Thread[npairs];
23     Thread[] as = new Reader[npairs];
24     for (int i = 0; i < npairs; ++i) {
25     ps[i] = new Writer();
26     as[i] = new Reader();
27     }
28     for (int i = 0; i < as.length; ++i) {
29     ps[i].start();
30     as[i].start();
31     }
32     }
33    
34 jsr166 1.3 static long nextRandom(long seed) {
35 dl 1.1 return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
36     }
37    
38     static long initialSeed(Object x) {
39     return (System.currentTimeMillis() + x.hashCode()) | 1;
40     }
41    
42     static class Writer extends Thread {
43     public void run() {
44     long s = initialSeed(this);
45     int n = iters / 2;
46     Long[] ns = nums;
47    
48     while (n-- > 0) {
49 jsr166 1.4 // int k = (int) (s & (LEN-1));
50 dl 1.1 // if (k < 0 || k >= LEN) k = 1;
51     // int l = (k+1) & (LEN-1);
52     // if (l < 0 || l >= LEN) l = 0;
53 jsr166 1.5 // int k = (s & 1) == 0 ? 0 : 1;
54     // int l = (k == 0) ? 1 : 0;
55 dl 1.1 if ((s & (LEN-1)) == 0) {
56     n3 = n1;
57     n0 = new Long(s);
58     n2 = n1;
59     n1 = new Long(s);
60     }
61     else {
62     n3 = n0;
63     n1 = new Long(s);
64     n2 = n0;
65     n0 = new Long(s);
66     }
67     s = nextRandom(s);
68     if (s == 0) s = initialSeed(this);
69     }
70     done = true;
71     total += s;
72     }
73     }
74    
75     static class Reader extends Thread {
76     public void run() {
77     int n = iters;
78     long s = initialSeed(this);
79     while (s != 0 && n > 0) {
80 jsr166 1.4 long nexts; // = nums[(int) (s & (LEN-1))].longValue();
81 dl 1.1 if ((s & (LEN-1)) == 0)
82     nexts = n0.longValue();
83     else
84     nexts = n1.longValue();
85     if (nexts != 0) {
86     if ((s & 4) == 0)
87     nexts = n2.longValue();
88     else
89     nexts = n3.longValue();
90     }
91    
92 jsr166 1.3 if (nexts != s)
93 dl 1.1 --n;
94     else if (done)
95     break;
96     s = nexts;
97     }
98     done = true;
99     total += s;
100 jsr166 1.3 if (s == 0)
101 dl 1.1 throw new Error("Saw uninitialized value");
102     }
103     }
104     }