OversynchronizedTest.java revision 12745:f068a4ffddd2
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 PrintWriter.println(Object) oversynchronized, can deadlock
27*/
28
29import java.io.PrintWriter;
30
31public class OversynchronizedTest extends Thread {
32    private static PrintWriter writer = new PrintWriter(System.out);
33    private static TestObj testObj = new TestObj("This is a test.", writer);
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 PrintWriter.println might cause deadlock
41            //if the object has a synchronized toString() method.
42            //using PrintWriter.println(testObj.toString()) won't have a problem
43           writer.println(testObj);
44        }
45    }
46
47    public static void main(String args[]) throws Exception {
48        // should no NullPointerException
49        writer.println((Object)null);
50
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
67class TestObj {
68    String mStr;
69
70    TestObj(String str, PrintWriter writer) {
71        mStr = str;
72        this.writer = writer;
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 PrintWriter.println(testObj)
86        //called by other threads.
87        writer.println("In test().");
88    }
89
90    public synchronized String toString() {
91        writer.println("Calling toString\n");
92        return mStr;
93    }
94
95    private PrintWriter writer;
96}
97