< prev index next >

src/java.base/share/classes/java/util/concurrent/ThreadPoolExecutor.java

Print this page
8221892: ThreadPoolExecutor: Thread.isAlive() is not equivalent to not being startable
Reviewed-by: martin, dholmes


 905             }
 906         }
 907 
 908         boolean workerStarted = false;
 909         boolean workerAdded = false;
 910         Worker w = null;
 911         try {
 912             w = new Worker(firstTask);
 913             final Thread t = w.thread;
 914             if (t != null) {
 915                 final ReentrantLock mainLock = this.mainLock;
 916                 mainLock.lock();
 917                 try {
 918                     // Recheck while holding lock.
 919                     // Back out on ThreadFactory failure or if
 920                     // shut down before lock acquired.
 921                     int c = ctl.get();
 922 
 923                     if (isRunning(c) ||
 924                         (runStateLessThan(c, STOP) && firstTask == null)) {
 925                         if (t.isAlive()) // precheck that t is startable
 926                             throw new IllegalThreadStateException();
 927                         workers.add(w);

 928                         int s = workers.size();
 929                         if (s > largestPoolSize)
 930                             largestPoolSize = s;
 931                         workerAdded = true;
 932                     }
 933                 } finally {
 934                     mainLock.unlock();
 935                 }
 936                 if (workerAdded) {
 937                     t.start();
 938                     workerStarted = true;
 939                 }
 940             }
 941         } finally {
 942             if (! workerStarted)
 943                 addWorkerFailed(w);
 944         }
 945         return workerStarted;
 946     }
 947 
 948     /**
 949      * Rolls back the worker thread creation.
 950      * - removes worker from workers, if present
 951      * - decrements worker count




 905             }
 906         }
 907 
 908         boolean workerStarted = false;
 909         boolean workerAdded = false;
 910         Worker w = null;
 911         try {
 912             w = new Worker(firstTask);
 913             final Thread t = w.thread;
 914             if (t != null) {
 915                 final ReentrantLock mainLock = this.mainLock;
 916                 mainLock.lock();
 917                 try {
 918                     // Recheck while holding lock.
 919                     // Back out on ThreadFactory failure or if
 920                     // shut down before lock acquired.
 921                     int c = ctl.get();
 922 
 923                     if (isRunning(c) ||
 924                         (runStateLessThan(c, STOP) && firstTask == null)) {
 925                         if (t.getState() != Thread.State.NEW)
 926                             throw new IllegalThreadStateException();
 927                         workers.add(w);
 928                         workerAdded = true;
 929                         int s = workers.size();
 930                         if (s > largestPoolSize)
 931                             largestPoolSize = s;

 932                     }
 933                 } finally {
 934                     mainLock.unlock();
 935                 }
 936                 if (workerAdded) {
 937                     t.start();
 938                     workerStarted = true;
 939                 }
 940             }
 941         } finally {
 942             if (! workerStarted)
 943                 addWorkerFailed(w);
 944         }
 945         return workerStarted;
 946     }
 947 
 948     /**
 949      * Rolls back the worker thread creation.
 950      * - removes worker from workers, if present
 951      * - decrements worker count


< prev index next >