/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4530538
* @summary Basic unit test of ThreadInfo.getLockName()
* and ThreadInfo.getLockOwnerName()
* @author Mandy Chung
*
* @build ThreadExecutionSynchronizer
*/
public class Locks {
private static boolean testFailed = false;
}
int retryCount=0;
if (retryCount++ > 500) {
" is expected to block on " + expectedLock +
" but got " + result +
}
goSleep(100);
}
}
" is expected to wait on " + expectedLock +
" but got " + result +
}
}
}
}
}
try {
} catch (InterruptedException e) {
e.printStackTrace();
testFailed = true;
}
}
public LockAThread() {
super("LockAThread");
}
public void run() {
synchronized(objA) {
// stop here for LockBThread to hold objB
synchronized(objB) {};
}
// The state could be anything. The expected state value
// passed with this method is not verified.
}
}
public LockBThread() {
super("LockBThread");
}
public void run() {
synchronized(objB) {
// signal waiting LockAThread.
// Signal main thread about to block on objC
synchronized(objC) {};
}
// The state could be anything. The expected state value
// passed with this method is not verified.
}
public void aboutToLockC() {
// Stop here till LockBThread about to blocked
// for lock objC.
goSleep(500);
}
}
public WaitingThread() {
super("WaitingThread");
}
public void run() {
synchronized(objC) {
try {
// Signal checker thread, about to wait on objC.
} catch (InterruptedException e) {
e.printStackTrace();
testFailed = true;
}
// block until CheckerThread finishes checking
// signal checker thread that it is about acquire
// object ready.
synchronized(ready) {};
}
synchronized(objC) {
try {
// signal checker thread, about to wait on objC
} catch (InterruptedException e) {
e.printStackTrace();
testFailed = true;
}
}
}
}
public CheckerThread() {
super("CheckerThread");
}
public void run() {
synchronized (ready) {
// wait until WaitingThread about to wait for objC
int retryCount = 0;
&& retryCount++ < 500) {
goSleep(100);
}
synchronized (objC) {
}
// wait for waiter thread to about to enter
// synchronized object ready.
// give chance for waiter thread to get blocked on
// object ready.
goSleep(50);
}
// wait for signal from waiting thread that it is about
// wait for objC.
synchronized(objC) {
}
}
}
// Test uncontested case
synchronized(objC) {
// The state could be anything. The expected state value
// passed with this method is not verified.
// Test deadlock case
// t1 holds lockA and attempts to lock B
// t2 holds lockB and attempts to lock C
t1 = new LockAThread();
t2 = new LockBThread();
t2.aboutToLockC();
long[] expectedThreads = new long[3];
}
goSleep(100);
// Test Object.wait() case
waiter = new WaitingThread();
checker = new CheckerThread();
try {
} catch (InterruptedException e) {
e.printStackTrace();
testFailed = true;
}
if (testFailed) {
throw new RuntimeException("TEST FAILED.");
}
}
throws Exception {
if (threadId == -1) {
throw new RuntimeException("TEST FAILED: " +
lock + " expected to have owner");
}
break;
}
}
}
}
return ownerInfo;
}
throws Exception {
// Check with ThreadInfo with no stack trace (i.e. no safepoint)
// Check with ThreadInfo with stack trace
}
throws Exception {
// Find the thread who is blocking on lock
" blocked on " + blockedLock);
}
}
long[] threads = new long[10];
int count = 0;
ownerInfo.getThreadId() +
" blocked on " + lock);
}
throw new RuntimeException("TEST FAILED: " +
"Expected chain of threads not matched; current count =" + count);
}
for (int i = 0; i < count; i++) {
if (threads[i] != expectedThreads[i]) {
"Unexpected thread in the chain " + threads[i] +
" expected to be " + expectedThreads[i]);
}
}
}
}