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