author | ctornqvi |
Thu, 16 May 2013 15:31:00 +0200 | |
changeset 17598 | f78d22d3c6c2 |
parent 9242 | ef138d47df58 |
child 30046 | cf2c86e1819e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
5506 | 18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
2 | 21 |
*/ |
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
7803
diff
changeset
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 32 |
*/ |
33 |
||
34 |
/* |
|
35 |
* @test |
|
5468
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
36 |
* @bug 4949279 6937857 |
2 | 37 |
* @summary Independent instantiations of Random() have distinct seeds. |
38 |
*/ |
|
39 |
||
5468
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
40 |
import java.util.ArrayList; |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
41 |
import java.util.HashSet; |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
42 |
import java.util.List; |
2 | 43 |
import java.util.Random; |
44 |
||
45 |
public class DistinctSeeds { |
|
46 |
public static void main(String[] args) throws Exception { |
|
47 |
// Strictly speaking, it is possible for these to randomly fail, |
|
5468
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
48 |
// but the probability should be small (approximately 2**-48). |
2 | 49 |
if (new Random().nextLong() == new Random().nextLong() || |
50 |
new Random().nextLong() == new Random().nextLong()) |
|
51 |
throw new RuntimeException("Random() seeds not unique."); |
|
5468
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
52 |
|
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
53 |
// Now try generating seeds concurrently |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
54 |
class RandomCollector implements Runnable { |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
55 |
long[] randoms = new long[1<<17]; |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
56 |
public void run() { |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
57 |
for (int i = 0; i < randoms.length; i++) |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
58 |
randoms[i] = new Random().nextLong(); |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
59 |
} |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
60 |
} |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
61 |
final int threadCount = 2; |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
62 |
List<RandomCollector> collectors = new ArrayList<>(); |
5468
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
63 |
List<Thread> threads = new ArrayList<Thread>(); |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
64 |
for (int i = 0; i < threadCount; i++) { |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
65 |
RandomCollector r = new RandomCollector(); |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
66 |
collectors.add(r); |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
67 |
threads.add(new Thread(r)); |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
68 |
} |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
69 |
for (Thread thread : threads) |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
70 |
thread.start(); |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
71 |
for (Thread thread : threads) |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
72 |
thread.join(); |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
73 |
int collisions = 0; |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
74 |
HashSet<Long> s = new HashSet<Long>(); |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
75 |
for (RandomCollector r : collectors) { |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
76 |
for (long x : r.randoms) { |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
77 |
if (s.contains(x)) |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
78 |
collisions++; |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
79 |
s.add(x); |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
80 |
} |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
81 |
} |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
82 |
System.out.printf("collisions=%d%n", collisions); |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
83 |
if (collisions > 10) |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
2
diff
changeset
|
84 |
throw new Error("too many collisions"); |
2 | 85 |
} |
86 |
} |