jdk/test/java/lang/Thread/ThreadStateTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main ThreadStateTest
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.concurrent.Semaphore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class ThreadStateTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static boolean testFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static class Lock {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        Lock(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static Lock globalLock = new Lock("my lock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static void main(String[] argv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        // Call Thread.getState to force all initialization done
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        // before test verification begins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        Thread.currentThread().getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        MyThread myThread = new MyThread("MyThread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // before myThread starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        checkThreadState(myThread, Thread.State.NEW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        myThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        myThread.waitUntilStarted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        checkThreadState(myThread, Thread.State.RUNNABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        synchronized (globalLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            myThread.goBlocked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            checkThreadState(myThread, Thread.State.BLOCKED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        myThread.goWaiting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        checkThreadState(myThread, Thread.State.WAITING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        myThread.goTimedWaiting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        checkThreadState(myThread, Thread.State.TIMED_WAITING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
       *********** park and parkUntil seems not working
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
       * ignore this case for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
       * Bug ID 5062095
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
       ***********************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        myThread.goParked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        checkThreadState(myThread, Thread.State.WAITING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        myThread.goTimedParked();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        myThread.goSleeping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        checkThreadState(myThread, Thread.State.TIMED_WAITING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        myThread.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        checkThreadState(myThread, Thread.State.TERMINATED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            myThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (testFailed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new RuntimeException("TEST FAILED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static void checkThreadState(Thread t, Thread.State expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        Thread.State state = t.getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        System.out.println("Checking thread state " + state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (state == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new RuntimeException(t.getName() + " expected to have " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                expected + " but got null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (state != expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            throw new RuntimeException(t.getName() + " expected to have " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                expected + " but got " + state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private static String getLockName(Object lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (lock == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return lock.getClass().getName() + '@' +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            Integer.toHexString(System.identityHashCode(lock));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static void goSleep(long ms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            Thread.sleep(ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static class MyThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        private ThreadExecutionSynchronizer thrsync = new ThreadExecutionSynchronizer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        MyThread(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        private final int RUNNABLE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        private final int BLOCKED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        private final int WAITING = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        private final int TIMED_WAITING = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        private final int PARKED = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        private final int TIMED_PARKED = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        private final int SLEEPING = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        private final int TERMINATE = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        private int state = RUNNABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        private boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            // Signal main thread to continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            thrsync.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    case RUNNABLE: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        double sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        for (int i = 0; i < 1000; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                           double r = Math.random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                           double x = Math.pow(3, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                           sum += x - r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    case BLOCKED: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        // signal main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        thrsync.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        System.out.println("  myThread is going to block.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        synchronized (globalLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                            // finish blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                            state = RUNNABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    case WAITING: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        synchronized (globalLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                            // signal main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                            thrsync.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                            System.out.println("  myThread is going to wait.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                globalLock.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    case TIMED_WAITING: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        synchronized (globalLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                            // signal main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                            thrsync.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                            System.out.println("  myThread is going to timed wait.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                globalLock.wait(10000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    case PARKED: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        // signal main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        thrsync.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        System.out.println("  myThread is going to park.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        LockSupport.park();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        // give a chance for the main thread to block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        goSleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    case TIMED_PARKED: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        // signal main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        thrsync.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        System.out.println("  myThread is going to timed park.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        long deadline = System.currentTimeMillis() + 10000*1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        LockSupport.parkUntil(deadline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        // give a chance for the main thread to block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        goSleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    case SLEEPING: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        // signal main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        thrsync.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        System.out.println("  myThread is going to sleep.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                            Thread.sleep(1000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                            // finish sleeping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                            interrupted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    case TERMINATE: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        // signal main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        thrsync.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        public void waitUntilStarted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            // wait for MyThread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            thrsync.waitForSignal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            goSleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        public void goBlocked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            System.out.println("Waiting myThread to go blocked.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            setState(BLOCKED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            // wait for MyThread to get blocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            thrsync.waitForSignal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            goSleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        public void goWaiting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            System.out.println("Waiting myThread to go waiting.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            setState(WAITING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            // wait for  MyThread to wait on object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            thrsync.waitForSignal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            goSleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        public void goTimedWaiting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            System.out.println("Waiting myThread to go timed waiting.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            setState(TIMED_WAITING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            // wait for MyThread timed wait call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            thrsync.waitForSignal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            goSleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        public void goParked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            System.out.println("Waiting myThread to go parked.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            setState(PARKED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            // wait for  MyThread state change to PARKED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            thrsync.waitForSignal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            goSleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        public void goTimedParked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            System.out.println("Waiting myThread to go timed parked.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            setState(TIMED_PARKED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // wait for  MyThread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            thrsync.waitForSignal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            goSleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        public void goSleeping() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            System.out.println("Waiting myThread to go sleeping.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            setState(SLEEPING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            // wait for  MyThread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            thrsync.waitForSignal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            goSleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        public void terminate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            System.out.println("Waiting myThread to terminate.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            setState(TERMINATE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            // wait for  MyThread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            thrsync.waitForSignal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            goSleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        private void setState(int newState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                case BLOCKED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    while (state == BLOCKED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        goSleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                case WAITING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                case TIMED_WAITING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    synchronized (globalLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        globalLock.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                case PARKED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                case TIMED_PARKED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    LockSupport.unpark(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                case SLEEPING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    this.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    static class ThreadExecutionSynchronizer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        private boolean  waiting;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        private Semaphore semaphore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        public ThreadExecutionSynchronizer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            semaphore = new Semaphore(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        waiting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        // Synchronizes two threads execution points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        // Basically any thread could get scheduled to run and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // it is not possible to know which thread reaches expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        // execution point. So whichever thread reaches a execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        // point first wait for the second thread. When the second thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        // reaches the expected execution point will wake up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        // the thread which is waiting here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        void stopOrGo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        semaphore.acquireUninterruptibly(); // Thread can get blocked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (!waiting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            waiting = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            // Wait for second thread to enter this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            while(!semaphore.hasQueuedThreads()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    Thread.sleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                } catch (InterruptedException xx) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            semaphore.release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            waiting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            semaphore.release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        // Wrapper function just for code readability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        void waitForSignal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        stopOrGo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        void signal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        stopOrGo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
}