Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/PrintStream/OversynchronizedTest.java
41149 views
1
/*
2
* Copyright (c) 2005, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/* @test
25
@bug 4905777
26
@summary PrintStream.println(Object) oversynchronized, can deadlock
27
@key randomness
28
*/
29
30
import java.io.PrintStream;
31
32
public class OversynchronizedTest extends Thread {
33
private static TestObj testObj = new TestObj("This is a test.");
34
private static int loopNum = 100;
35
36
public void run() {
37
for(int i=0; i<loopNum; i++) {
38
testObj.test();
39
40
//passing an object to System.out.println might cause deadlock
41
//if the object has a synchronized toString() method.
42
//using System.out.println(testObj.toString()) won't have a problem
43
System.out.println(testObj);
44
}
45
}
46
47
public static void main(String args[]) throws Exception {
48
// should no NullPointerException
49
System.out.println((Object)null);
50
51
// over synch test
52
int num = 5;
53
54
OversynchronizedTest[] t = new OversynchronizedTest[num];
55
for(int i=0; i<num; i++) {
56
t[i] = new OversynchronizedTest();
57
t[i].start();
58
}
59
60
for(int i=0; i <num; i++) {
61
t[i].join();
62
}
63
64
System.out.println("Test completed.");
65
}
66
}
67
68
class TestObj {
69
String mStr;
70
71
TestObj(String str) {
72
mStr = str;
73
}
74
75
synchronized void test() {
76
try {
77
long t = Math.round(Math.random()*10);
78
Thread.currentThread().sleep(t);
79
} catch (InterruptedException e) {
80
// jtreg timeout?
81
// Only jtreg will interrupt this thread so it knows what to do:
82
e.printStackTrace();
83
}
84
85
//the following line might cause hang if there is System.out.println(testObj)
86
//called by other threads.
87
System.out.println("In test().");
88
}
89
90
public synchronized String toString() {
91
System.out.println("Calling toString\n");
92
return mStr;
93
}
94
}
95
96