1 |
/* |
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 |
* http://creativecommons.org/licenses/publicdomain |
5 |
*/ |
6 |
|
7 |
public class FinalLongTest { |
8 |
static int npairs = 2; |
9 |
static int iters = 10000000; |
10 |
static int LEN = 2; |
11 |
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 |
|
19 |
|
20 |
public static void main(String[] args) { |
21 |
for (int i = 0; i < LEN; ++i) |
22 |
nums[i] = new Long(i+1); |
23 |
Thread[] ps = new Thread[npairs]; |
24 |
Thread[] as = new Reader[npairs]; |
25 |
for (int i = 0; i < npairs; ++i) { |
26 |
ps[i] = new Writer(); |
27 |
as[i] = new Reader(); |
28 |
} |
29 |
for (int i = 0; i < as.length; ++i) { |
30 |
ps[i].start(); |
31 |
as[i].start(); |
32 |
} |
33 |
} |
34 |
|
35 |
static long nextRandom(long seed) { |
36 |
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1); |
37 |
} |
38 |
|
39 |
static long initialSeed(Object x) { |
40 |
return (System.currentTimeMillis() + x.hashCode()) | 1; |
41 |
} |
42 |
|
43 |
static class Writer extends Thread { |
44 |
public void run() { |
45 |
long s = initialSeed(this); |
46 |
int n = iters / 2; |
47 |
Long[] ns = nums; |
48 |
|
49 |
while (n-- > 0) { |
50 |
// int k = (int) (s & (LEN-1)); |
51 |
// if (k < 0 || k >= LEN) k = 1; |
52 |
// int l = (k+1) & (LEN-1); |
53 |
// if (l < 0 || l >= LEN) l = 0; |
54 |
// int k = (s & 1) == 0? 0 : 1; |
55 |
// int l = (k == 0)? 1 : 0; |
56 |
if ((s & (LEN-1)) == 0) { |
57 |
n3 = n1; |
58 |
n0 = new Long(s); |
59 |
n2 = n1; |
60 |
n1 = new Long(s); |
61 |
} |
62 |
else { |
63 |
n3 = n0; |
64 |
n1 = new Long(s); |
65 |
n2 = n0; |
66 |
n0 = new Long(s); |
67 |
} |
68 |
s = nextRandom(s); |
69 |
if (s == 0) s = initialSeed(this); |
70 |
} |
71 |
done = true; |
72 |
total += s; |
73 |
} |
74 |
} |
75 |
|
76 |
static class Reader extends Thread { |
77 |
public void run() { |
78 |
int n = iters; |
79 |
long s = initialSeed(this); |
80 |
while (s != 0 && n > 0) { |
81 |
long nexts; // = nums[(int) (s & (LEN-1))].longValue(); |
82 |
if ((s & (LEN-1)) == 0) |
83 |
nexts = n0.longValue(); |
84 |
else |
85 |
nexts = n1.longValue(); |
86 |
if (nexts != 0) { |
87 |
if ((s & 4) == 0) |
88 |
nexts = n2.longValue(); |
89 |
else |
90 |
nexts = n3.longValue(); |
91 |
} |
92 |
|
93 |
if (nexts != s) |
94 |
--n; |
95 |
else if (done) |
96 |
break; |
97 |
s = nexts; |
98 |
} |
99 |
done = true; |
100 |
total += s; |
101 |
if (s == 0) |
102 |
throw new Error("Saw uninitialized value"); |
103 |
} |
104 |
} |
105 |
} |