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
19package org.apache.harmony.jpda.tests.jdwp.Events;
20
21import org.apache.harmony.jpda.tests.framework.LogWriter;
22import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
23import org.apache.harmony.jpda.tests.share.SyncDebuggee;
24
25public class MonitorContendedEnterAndEnteredDebuggee extends SyncDebuggee {
26
27    static final String TESTED_THREAD = "BLOCKED_THREAD";
28
29    static Object lock = new MonitorWaitMockMonitor();
30
31    BlockedThread thread;
32
33    @Override
34    public void run() {
35        thread = new BlockedThread(logWriter,TESTED_THREAD);
36
37        // Inform debugger test is ready for testing
38        logWriter.println("--> Main thread : started");
39        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
40
41        synchronized (lock) {
42            // Request has been set by Debugger
43            synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
44
45            logWriter.println("main thread: start tested thread");
46            thread.start();
47
48            // wait and hold the lock until the second thread blocks
49            while (!thread.getState().equals(Thread.State.valueOf("BLOCKED"))){
50                Thread.yield();
51                logWriter.println("main thread: Waiting for second thread to attempt to lock a monitor");
52            }
53
54            logWriter.println("--> main thread: finish test");
55        }
56    }
57
58    static class BlockedThread extends Thread {
59        private LogWriter logWriter;
60
61        public BlockedThread(LogWriter writer, String name){
62            logWriter = writer;
63            this.setName(name);
64        }
65
66        @Override
67        public void run() {
68            logWriter.println("--> BlockedThread: start to run");
69
70            synchronized (lock) {
71                this.getName().trim();
72                logWriter.println("--> BlockedThread: get lock");
73            }
74        }
75    }
76
77    public static void main(String[] args) {
78        runDebuggee(MonitorContendedEnterAndEnteredDebuggee.class);
79    }
80
81}
82
83