001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.drlvm.tests.regression.h3256;
019:
020: public class InterruptTimedWaitingTest {
021:
022: Object lock = new Object();
023: int threadCount = 100;
024: int THREAD_WAIT_TIME = 10000;
025: int WAIT_CONDITION_TIME = 2000;
026: int SLEEP_TIME = 100;
027: int loopCountBegin = WAIT_CONDITION_TIME / SLEEP_TIME;
028: int loopCount;
029: int waitedTime;
030:
031: class ThreadWaiting extends Thread {
032: volatile boolean exceptionReceived = false;
033: volatile boolean working = false;
034:
035: public void run() {
036: synchronized (lock) {
037: this .working = true;
038: lock.notify();
039: }
040: synchronized (this ) {
041: try {
042: this .wait(THREAD_WAIT_TIME);
043: } catch (InterruptedException e) {
044: exceptionReceived = true;
045: }
046: }
047: }
048: }
049:
050: public void testInterrupt_Waiting() {
051: for (int i = 0; i < threadCount; i++) {
052: ThreadWaiting t = new ThreadWaiting();
053: try {
054: synchronized (lock) {
055: t.start();
056: while (!t.working) {
057: lock.wait();
058: }
059: }
060: } catch (InterruptedException e) {
061: e.printStackTrace();
062: }
063:
064: // wait for Thread.State.TIMED_WAITING
065: Thread.State ts = t.getState();
066: loopCount = loopCountBegin;
067: while ((ts != Thread.State.TIMED_WAITING)
068: && (loopCount-- > 0)) {
069: ts = t.getState();
070: try {
071: Thread.sleep(SLEEP_TIME);
072: } catch (Exception e) {
073: e.printStackTrace();
074: }
075: }
076:
077: // interrupt the thread
078: t.interrupt();
079:
080: // wait for InteruptedException
081: loopCount = loopCountBegin;
082: while (!t.exceptionReceived && (loopCount-- > 0)) {
083: try {
084: Thread.sleep(SLEEP_TIME);
085: } catch (Exception e) {
086: e.printStackTrace();
087: }
088: }
089: waitedTime = (loopCountBegin - loopCount) * SLEEP_TIME;
090: System.out.println(i + " exception waited for "
091: + waitedTime + " ms");
092:
093: // check for exception received
094: if (loopCount < 0) {
095: System.out
096: .println(i
097: + " FAILED: waiting thread has not received the InterruptedException");
098: System.exit(-1);
099: }
100: // check for interrupted status cleared
101: if (t.isInterrupted()) {
102: System.out
103: .println(i
104: + " FAILED: interrupt status has not been cleared");
105: System.exit(-2);
106: }
107: }
108: }
109:
110: public static void main(String args[]) {
111: new InterruptTimedWaitingTest().testInterrupt_Waiting();
112: }
113: }
|