jdk/test/java/lang/Thread/ThreadStateTest.java
author morris
Fri, 11 Oct 2013 12:40:14 -0700
changeset 21219 0c61684d9606
parent 10065 f0dd422f49fa
child 21637 fcaf599f308f
permissions -rw-r--r--
7195597: ThreadStateTest gets different results with -Xcomp Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
     2
 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     5014783
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic unit test of thread states returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          Thread.getState().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @build ThreadStateTest
21219
0c61684d9606 7195597: ThreadStateTest gets different results with -Xcomp
morris
parents: 10065
diff changeset
    33
 * @run main/othervm -Xmixed ThreadStateTest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.concurrent.locks.LockSupport;
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
    37
import java.util.concurrent.Phaser;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class ThreadStateTest {
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
    40
    // maximum number of retries when checking for thread state.
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
    41
    static final int MAX_RETRY = 500;
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
    42
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static boolean testFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
    45
    // used to achieve waiting states
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
    46
    static final Object globalLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static void main(String[] argv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        // Call Thread.getState to force all initialization done
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        // before test verification begins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        Thread.currentThread().getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        MyThread myThread = new MyThread("MyThread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        // before myThread starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        checkThreadState(myThread, Thread.State.NEW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        myThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        myThread.waitUntilStarted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        checkThreadState(myThread, Thread.State.RUNNABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        synchronized (globalLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            myThread.goBlocked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            checkThreadState(myThread, Thread.State.BLOCKED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        myThread.goWaiting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        checkThreadState(myThread, Thread.State.WAITING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        myThread.goTimedWaiting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        checkThreadState(myThread, Thread.State.TIMED_WAITING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
       *********** park and parkUntil seems not working
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
       * ignore this case for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
       * Bug ID 5062095
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
       ***********************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        myThread.goParked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        checkThreadState(myThread, Thread.State.WAITING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        myThread.goTimedParked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        checkThreadState(myThread, Thread.State.TIMED_WAITING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        myThread.goSleeping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        checkThreadState(myThread, Thread.State.TIMED_WAITING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        myThread.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        checkThreadState(myThread, Thread.State.TERMINATED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            myThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   100
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (testFailed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new RuntimeException("TEST FAILED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static void checkThreadState(Thread t, Thread.State expected) {
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   107
        // wait for the thread to transition to the expected state.
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   108
        // There is a small window between the thread checking the state
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   109
        // and the thread actual entering that state.
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   110
        Thread.State state;
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   111
        int retryCount=0;
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   112
        while ((state = t.getState()) != expected && retryCount < MAX_RETRY) {
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   113
            if (state != Thread.State.RUNNABLE) {
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   114
                throw new RuntimeException("Thread not in expected state yet," +
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   115
                        " but it should at least be RUNNABLE");
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   116
            }
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   117
            goSleep(10);
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   118
            retryCount++;
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   119
        }
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   120
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        System.out.println("Checking thread state " + state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (state == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throw new RuntimeException(t.getName() + " expected to have " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                expected + " but got null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (state != expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw new RuntimeException(t.getName() + " expected to have " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                expected + " but got " + state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static void goSleep(long ms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            Thread.sleep(ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    static class MyThread extends Thread {
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   144
        // Phaser to sync between the main thread putting
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   145
        // this thread into various states
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   146
        private Phaser phaser =  new Phaser(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        MyThread(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        private final int RUNNABLE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        private final int BLOCKED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        private final int WAITING = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        private final int TIMED_WAITING = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        private final int PARKED = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        private final int TIMED_PARKED = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        private final int SLEEPING = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        private final int TERMINATE = 7;
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   160
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   161
        private volatile int state = RUNNABLE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        private boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            // Signal main thread to continue.
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   166
            phaser.arriveAndAwaitAdvance();
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   167
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    case RUNNABLE: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        double sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        for (int i = 0; i < 1000; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                           double r = Math.random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                           double x = Math.pow(3, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                           sum += x - r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    case BLOCKED: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        // signal main thread.
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   181
                        phaser.arrive();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        System.out.println("  myThread is going to block.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        synchronized (globalLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                            // finish blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                            state = RUNNABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    case WAITING: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        synchronized (globalLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                            // signal main thread.
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   192
                            phaser.arrive();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                            System.out.println("  myThread is going to wait.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                globalLock.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    case TIMED_WAITING: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        synchronized (globalLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                            // signal main thread.
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   205
                            phaser.arrive();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                            System.out.println("  myThread is going to timed wait.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                globalLock.wait(10000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    case PARKED: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        // signal main thread.
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   217
                        phaser.arrive();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        System.out.println("  myThread is going to park.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        LockSupport.park();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        // give a chance for the main thread to block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        goSleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    case TIMED_PARKED: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        // signal main thread.
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   226
                        phaser.arrive();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                        System.out.println("  myThread is going to timed park.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        long deadline = System.currentTimeMillis() + 10000*1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                        LockSupport.parkUntil(deadline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        // give a chance for the main thread to block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        goSleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    case SLEEPING: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        // signal main thread.
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   237
                        phaser.arrive();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        System.out.println("  myThread is going to sleep.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                            Thread.sleep(1000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                            // finish sleeping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    case TERMINATE: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        // signal main thread.
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   249
                        phaser.arrive();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   257
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        public void waitUntilStarted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // wait for MyThread.
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   260
            phaser.arriveAndAwaitAdvance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        public void goBlocked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            System.out.println("Waiting myThread to go blocked.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            setState(BLOCKED);
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   266
            // wait for MyThread to get to a point just before being blocked
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   267
            phaser.arriveAndAwaitAdvance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        public void goWaiting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            System.out.println("Waiting myThread to go waiting.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            setState(WAITING);
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   273
            // wait for MyThread to get to just before wait on object.
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   274
            phaser.arriveAndAwaitAdvance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   276
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        public void goTimedWaiting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            System.out.println("Waiting myThread to go timed waiting.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            setState(TIMED_WAITING);
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   280
            // wait for MyThread to get to just before timed wait call.
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   281
            phaser.arriveAndAwaitAdvance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   283
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        public void goParked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            System.out.println("Waiting myThread to go parked.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            setState(PARKED);
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   287
            // wait for MyThread to get to just before parked.
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   288
            phaser.arriveAndAwaitAdvance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   290
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        public void goTimedParked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            System.out.println("Waiting myThread to go timed parked.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            setState(TIMED_PARKED);
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   294
            // wait for MyThread to get to just before timed park.
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   295
            phaser.arriveAndAwaitAdvance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        public void goSleeping() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            System.out.println("Waiting myThread to go sleeping.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            setState(SLEEPING);
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   301
            // wait for MyThread to get to just before sleeping
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   302
            phaser.arriveAndAwaitAdvance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   304
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        public void terminate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            System.out.println("Waiting myThread to terminate.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            setState(TERMINATE);
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   308
            // wait for MyThread to get to just before terminate
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   309
            phaser.arriveAndAwaitAdvance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        private void setState(int newState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                case BLOCKED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    while (state == BLOCKED) {
10065
f0dd422f49fa 7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents: 5506
diff changeset
   316
                        goSleep(10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                case WAITING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                case TIMED_WAITING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    synchronized (globalLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        globalLock.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                case PARKED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                case TIMED_PARKED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    LockSupport.unpark(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                case SLEEPING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    this.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
}