1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *
15 *  See the License for the specific language governing permissions and
16 *  limitations under the License.
17 */
18
19/**
20 * @author Vitaly A. Provodin
21 */
22
23/**
24 * Created on 24.02.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
27
28import org.apache.harmony.jpda.tests.framework.DebuggeeSynchronizer;
29import org.apache.harmony.jpda.tests.framework.LogWriter;
30import org.apache.harmony.jpda.tests.framework.TestErrorException;
31import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
32import org.apache.harmony.jpda.tests.share.SyncDebuggee;
33
34/**
35 * The class specifies debuggee for
36 * <code>org.apache.harmony.jpda.tests.jdwp.ThreadReference.CurrentContendedMonitorTest</code>.
37 * This debuggee starts the tested thread <code>TESTED_THREAD</code> which
38 * invokes <code>wait()</code>.
39 */
40public class CurrentContendedMonitorDebuggee extends SyncDebuggee {
41
42    public static final String TESTED_THREAD = "TestedThread";
43
44    static Object waitForStart = new Object();
45
46    static Object waitForFinish = new Object();
47
48    DebuggeeThread thrd;
49
50    public void run() {
51        thrd = new DebuggeeThread(TESTED_THREAD, logWriter, synchronizer);
52        try {
53            synchronized (waitForStart) {
54                thrd.start();
55                try {
56                    waitForStart.wait();
57                } catch (InterruptedException e) {
58                    throw new TestErrorException(e);
59                }
60            }
61            logWriter.println("thread started");
62
63            synchronized (waitForStart) {
64                synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
65            }
66
67            synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
68        } finally {
69            if (thrd.isAlive()) {
70                logWriter.println("Thread is alive. Interrupt thread");
71                thrd.interrupt();
72            }
73        }
74    }
75
76    class DebuggeeThread extends Thread {
77
78        LogWriter logWriter;
79
80        DebuggeeSynchronizer synchronizer;
81
82        public DebuggeeThread(String name, LogWriter logWriter,
83                DebuggeeSynchronizer synchronizer) {
84            super(name);
85            this.logWriter = logWriter;
86            this.synchronizer = synchronizer;
87        }
88
89        public void run() {
90
91            synchronized (CurrentContendedMonitorDebuggee.waitForFinish) {
92
93                synchronized (CurrentContendedMonitorDebuggee.waitForStart) {
94                    CurrentContendedMonitorDebuggee.waitForStart.notifyAll();
95
96                    try {
97                        logWriter.println("Thread waits on object..");
98                        CurrentContendedMonitorDebuggee.waitForStart.wait();
99                    } catch (InterruptedException e) {
100                        logWriter.println("Expected " + e);
101                        //synchronizer.sendMessage(e.toString());
102                    }
103                }
104            }
105        }
106    }
107
108    public static void main(String[] args) {
109        runDebuggee(CurrentContendedMonitorDebuggee.class);
110    }
111}
112