001/*
002 * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023
024/*
025 */
026
027package com.oracle.graal.jtt.threads;
028
029import org.junit.*;
030
031import com.oracle.graal.jtt.*;
032
033//Test all, mainly monitors
034public class Thread_isInterrupted02 extends JTTTest {
035
036    private static final Object start = new Object();
037    private static final Object end = new Object();
038    private static int waitTime;
039
040    @SuppressWarnings("unused")
041    public static boolean test(int i, int time) throws InterruptedException {
042        waitTime = time;
043        final Thread thread = new Thread();
044        synchronized (thread) {
045            // start the thread and wait for it
046            thread.setDaemon(true); // in case the thread gets stuck
047            thread.start();
048            thread.wait();
049        }
050        synchronized (start) {
051            thread.interrupt();
052        }
053        synchronized (end) {
054            end.wait(200);
055        }
056        return thread.interrupted;
057    }
058
059    private static class Thread extends java.lang.Thread {
060
061        private boolean interrupted;
062
063        @Override
064        public void run() {
065            try {
066                synchronized (start) {
067                    synchronized (this) {
068                        // signal test thread that we are running
069                        notify();
070                    }
071                    // wait for the condition, which should be interrupted
072                    if (waitTime == 0) {
073                        start.wait();
074                    } else {
075                        start.wait(waitTime);
076                    }
077                }
078            } catch (InterruptedException e) {
079                // interrupted successfully.
080                interrupted = true;
081                synchronized (end) {
082                    // notify the other thread we are done
083                    end.notify();
084                }
085            }
086        }
087    }
088
089    @Test(timeout = 20000)
090    public void run0() throws Throwable {
091        runTest("test", 0, 0);
092    }
093
094    @Test(timeout = 20000)
095    public void run1() throws Throwable {
096        runTest("test", 1, 500);
097    }
098
099}