author | lana |
Thu, 25 Oct 2012 20:04:23 -0700 | |
changeset 14110 | 0e52e8a05d0d |
parent 13486 | 4f0635e148c1 |
child 15212 | bbda5353d846 |
permissions | -rw-r--r-- |
13486
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
1 |
/* |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
2 |
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
4 |
* |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
8 |
* |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
13 |
* accompanied this code). |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
14 |
* |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
18 |
* |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
21 |
* questions. |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
22 |
* |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
23 |
*/ |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
24 |
|
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
25 |
/* |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
26 |
* Manual test |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
27 |
*/ |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
28 |
|
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
29 |
import java.lang.ref.*; |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
30 |
|
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
31 |
public class Test7190310 { |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
32 |
private static Object str = new Object() { |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
33 |
public String toString() { |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
34 |
return "The Object"; |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
35 |
} |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
36 |
|
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
37 |
protected void finalize() throws Throwable { |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
38 |
System.out.println("The Object is being finalized"); |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
39 |
super.finalize(); |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
40 |
} |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
41 |
}; |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
42 |
private final static ReferenceQueue<Object> rq = |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
43 |
new ReferenceQueue<Object>(); |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
44 |
private final static WeakReference<Object> wr = |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
45 |
new WeakReference<Object>(str, rq); |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
46 |
|
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
47 |
public static void main(String[] args) |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
48 |
throws InterruptedException { |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
49 |
Thread reader = new Thread() { |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
50 |
public void run() { |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
51 |
while (wr.get() != null) { |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
52 |
} |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
53 |
System.out.println("wr.get() returned null"); |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
54 |
} |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
55 |
}; |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
56 |
|
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
57 |
Thread queueReader = new Thread() { |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
58 |
public void run() { |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
59 |
try { |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
60 |
Reference<? extends Object> ref = rq.remove(); |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
61 |
System.out.println(ref); |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
62 |
System.out.println("queueReader returned, ref==wr is " |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
63 |
+ (ref == wr)); |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
64 |
} catch (InterruptedException e) { |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
65 |
System.err.println("Sleep interrupted - exiting"); |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
66 |
} |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
67 |
} |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
68 |
}; |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
69 |
|
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
70 |
reader.start(); |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
71 |
queueReader.start(); |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
72 |
|
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
73 |
Thread.sleep(1000); |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
74 |
str = null; |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
75 |
System.gc(); |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
76 |
} |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
77 |
} |
4f0635e148c1
7190310: Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
kvn
parents:
diff
changeset
|
78 |