jdk/test/java/lang/Thread/ThreadStateController.java
changeset 30352 551e7993450a
parent 27340 d86f2d125146
child 40684 2e37c119dc2a
equal deleted inserted replaced
30351:94434dd8dc9f 30352:551e7993450a
     1 /*
     1 /*
     2  * Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2013, 2015 Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    26 import java.util.concurrent.TimeoutException;
    26 import java.util.concurrent.TimeoutException;
    27 import java.util.concurrent.atomic.AtomicInteger;
    27 import java.util.concurrent.atomic.AtomicInteger;
    28 import java.util.concurrent.locks.LockSupport;
    28 import java.util.concurrent.locks.LockSupport;
    29 
    29 
    30 import jdk.testlibrary.LockFreeLogManager;
    30 import jdk.testlibrary.LockFreeLogManager;
       
    31 import jdk.testlibrary.Utils;
    31 
    32 
    32 /**
    33 /**
    33  * ThreadStateController allows a thread to request this thread to transition
    34  * ThreadStateController allows a thread to request this thread to transition
    34  * to a specific thread state.  The {@linkplain #transitionTo request} is
    35  * to a specific thread state.  The {@linkplain #transitionTo request} is
    35  * a blocking call that the calling thread will wait until this thread is about
    36  * a blocking call that the calling thread will wait until this thread is about
    71         }
    72         }
    72     }
    73     }
    73 
    74 
    74     public static void pause(long ms) {
    75     public static void pause(long ms) {
    75         try {
    76         try {
    76             Thread.sleep(ms);
    77             Thread.sleep(Utils.adjustTimeout(ms));
    77         } catch (InterruptedException e) {
    78         } catch (InterruptedException e) {
    78             throw new RuntimeException(e);
    79             throw new RuntimeException(e);
    79         }
    80         }
    80     }
    81     }
    81 
    82 
   133                         log("%d:   %s acquired the lock (iterations %d)%n",
   134                         log("%d:   %s acquired the lock (iterations %d)%n",
   134                             getId(), getName(), iterations.get());
   135                             getId(), getName(), iterations.get());
   135                         try {
   136                         try {
   136                             // this thread has escaped the BLOCKED state
   137                             // this thread has escaped the BLOCKED state
   137                             // release the lock and a short wait before continue
   138                             // release the lock and a short wait before continue
   138                             lock.wait(10);
   139                             lock.wait(Utils.adjustTimeout(10));
   139                         } catch (InterruptedException e) {
   140                         } catch (InterruptedException e) {
   140                             // ignore
   141                             // ignore
   141                             interrupted.incrementAndGet();
   142                             interrupted.incrementAndGet();
   142                         }
   143                         }
   143                     }
   144                     }
   163                     synchronized (lock) {
   164                     synchronized (lock) {
   164                         log("%d: %s is going to timed waiting (iterations %d interrupted %d)%n",
   165                         log("%d: %s is going to timed waiting (iterations %d interrupted %d)%n",
   165                             getId(), getName(), iterations.get(), interrupted.get());
   166                             getId(), getName(), iterations.get(), interrupted.get());
   166                         try {
   167                         try {
   167                             stateChange(nextState);
   168                             stateChange(nextState);
   168                             lock.wait(10000);
   169                             lock.wait(Integer.MAX_VALUE);
   169                             log("%d:   %s wakes up from timed waiting (iterations %d interrupted %d)%n",
   170                             log("%d:   %s wakes up from timed waiting (iterations %d interrupted %d)%n",
   170                                 getId(), getName(), iterations.get(), interrupted.get());
   171                                 getId(), getName(), iterations.get(), interrupted.get());
   171                         } catch (InterruptedException e) {
   172                         } catch (InterruptedException e) {
   172                             // ignore
   173                             // ignore
   173                             interrupted.incrementAndGet();
   174                             interrupted.incrementAndGet();
   183                     break;
   184                     break;
   184                 }
   185                 }
   185                 case S_TIMED_PARKED: {
   186                 case S_TIMED_PARKED: {
   186                     log("%d: %s is going to timed park (iterations %d)%n",
   187                     log("%d: %s is going to timed park (iterations %d)%n",
   187                         getId(), getName(), iterations.get());
   188                         getId(), getName(), iterations.get());
   188                     long deadline = System.currentTimeMillis() + 10000*1000;
   189                     long deadline = System.currentTimeMillis() +
       
   190                                         Utils.adjustTimeout(10000*1000);
   189                     stateChange(nextState);
   191                     stateChange(nextState);
   190                     LockSupport.parkUntil(deadline);
   192                     LockSupport.parkUntil(deadline);
   191                     break;
   193                     break;
   192                 }
   194                 }
   193                 case S_SLEEPING: {
   195                 case S_SLEEPING: {
   194                     log("%d: %s is going to sleep (iterations %d interrupted %d)%n",
   196                     log("%d: %s is going to sleep (iterations %d interrupted %d)%n",
   195                         getId(), getName(), iterations.get(), interrupted.get());
   197                         getId(), getName(), iterations.get(), interrupted.get());
   196                     try {
   198                     try {
   197                         stateChange(nextState);
   199                         stateChange(nextState);
   198                         Thread.sleep(1000000);
   200                         Thread.sleep(Utils.adjustTimeout(1000000));
   199                     } catch (InterruptedException e) {
   201                     } catch (InterruptedException e) {
   200                         // finish sleeping
   202                         // finish sleeping
   201                         interrupted.incrementAndGet();
   203                         interrupted.incrementAndGet();
   202                     }
   204                     }
   203                     break;
   205                     break;