12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/*
22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2008 The Android Open Source Project
32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License.
62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at
72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software
112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and
142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License.
152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */
165d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
175d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
185d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao/**
195d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Exercise monitors.
205d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */
215d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaopublic class Monitor {
225d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public static int mVal = 0;
235d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
245d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public synchronized void subTest() {
255d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        Object obj = new Object();
265d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        synchronized (obj) {
275d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            mVal++;
285d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            obj = null;     // does NOT cause a failure on exit
29795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao            Main.assertTrue(obj == null);
305d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        }
315d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
325d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
335d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
345d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public static void run() {
355d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println("Monitor.run");
365d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
375d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        Object obj = null;
385d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
395d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        try {
405d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            synchronized (obj) {
415d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                mVal++;
425d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
43795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao            Main.assertTrue(false);
445d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        } catch (NullPointerException npe) {
455d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            /* expected */
465d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        }
475d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
485d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        obj = new Object();
495d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        synchronized (obj) {
505d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            mVal++;
515d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        }
525d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
535d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        new Monitor().subTest();
545d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
55795d78f4d04c8d007bf2bdf2ed4131379bcf19c7jeffhao        Main.assertTrue(mVal == 2);
565d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
575d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao}
58