2
|
1 |
/*
|
|
2 |
* Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
@bug 4905777
|
|
26 |
@summary PrintStream.println(Object) oversynchronized, can deadlock
|
|
27 |
*/
|
|
28 |
|
|
29 |
import java.io.PrintStream;
|
|
30 |
|
|
31 |
public class OversynchronizedTest extends Thread {
|
|
32 |
private static TestObj testObj = new TestObj("This is a test.");
|
|
33 |
private static int loopNum = 100;
|
|
34 |
|
|
35 |
public void run() {
|
|
36 |
for(int i=0; i<loopNum; i++) {
|
|
37 |
testObj.test();
|
|
38 |
|
|
39 |
//passing an object to System.out.println might cause deadlock
|
|
40 |
//if the object has a synchronized toString() method.
|
|
41 |
//using System.out.println(testObj.toString()) won't have a problem
|
|
42 |
System.out.println(testObj);
|
|
43 |
}
|
|
44 |
}
|
|
45 |
|
|
46 |
public static void main(String args[]) throws Exception {
|
|
47 |
// should no NullPointerException
|
|
48 |
System.out.println((Object)null);
|
|
49 |
|
|
50 |
// over synch test
|
|
51 |
int num = 5;
|
|
52 |
|
|
53 |
OversynchronizedTest[] t = new OversynchronizedTest[num];
|
|
54 |
for(int i=0; i<num; i++) {
|
|
55 |
t[i] = new OversynchronizedTest();
|
|
56 |
t[i].start();
|
|
57 |
}
|
|
58 |
|
|
59 |
for(int i=0; i <num; i++) {
|
|
60 |
t[i].join();
|
|
61 |
}
|
|
62 |
|
|
63 |
System.out.println("Test completed.");
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
|
67 |
class TestObj {
|
|
68 |
String mStr;
|
|
69 |
|
|
70 |
TestObj(String str) {
|
|
71 |
mStr = str;
|
|
72 |
}
|
|
73 |
|
|
74 |
synchronized void test() {
|
|
75 |
try {
|
|
76 |
long t = Math.round(Math.random()*10);
|
|
77 |
Thread.currentThread().sleep(t);
|
|
78 |
} catch (InterruptedException e) {
|
|
79 |
// jtreg timeout?
|
|
80 |
// Only jtreg will interrupt this thread so it knows what to do:
|
|
81 |
e.printStackTrace();
|
|
82 |
}
|
|
83 |
|
|
84 |
//the following line might cause hang if there is System.out.println(testObj)
|
|
85 |
//called by other threads.
|
|
86 |
System.out.println("In test().");
|
|
87 |
}
|
|
88 |
|
|
89 |
synchronized public String toString() {
|
|
90 |
System.out.println("Calling toString\n");
|
|
91 |
return mStr;
|
|
92 |
}
|
|
93 |
}
|