587 * instead interrupting a task being run. We implement a simple 588 * non-reentrant mutual exclusion lock rather than use 589 * ReentrantLock because we do not want worker tasks to be able to 590 * reacquire the lock when they invoke pool control methods like 591 * setCorePoolSize. Additionally, to suppress interrupts until 592 * the thread actually starts running tasks, we initialize lock 593 * state to a negative value, and clear it upon start (in 594 * runWorker). 595 */ 596 private final class Worker 597 extends AbstractQueuedSynchronizer 598 implements Runnable 599 { 600 /** 601 * This class will never be serialized, but we provide a 602 * serialVersionUID to suppress a javac warning. 603 */ 604 private static final long serialVersionUID = 6138294804551838833L; 605 606 /** Thread this worker is running in. Null if factory fails. */ 607 final Thread thread; 608 /** Initial task to run. Possibly null. */ 609 Runnable firstTask; 610 /** Per-thread task counter */ 611 volatile long completedTasks; 612 613 // TODO: switch to AbstractQueuedLongSynchronizer and move 614 // completedTasks into the lock word. 615 616 /** 617 * Creates with given first task and thread from ThreadFactory. 618 * @param firstTask the first task (null if none) 619 */ 620 Worker(Runnable firstTask) { 621 setState(-1); // inhibit interrupts until runWorker 622 this.firstTask = firstTask; 623 this.thread = getThreadFactory().newThread(this); 624 } 625 626 /** Delegates main run loop to outer runWorker. */ 627 public void run() { 628 runWorker(this); | 587 * instead interrupting a task being run. We implement a simple 588 * non-reentrant mutual exclusion lock rather than use 589 * ReentrantLock because we do not want worker tasks to be able to 590 * reacquire the lock when they invoke pool control methods like 591 * setCorePoolSize. Additionally, to suppress interrupts until 592 * the thread actually starts running tasks, we initialize lock 593 * state to a negative value, and clear it upon start (in 594 * runWorker). 595 */ 596 private final class Worker 597 extends AbstractQueuedSynchronizer 598 implements Runnable 599 { 600 /** 601 * This class will never be serialized, but we provide a 602 * serialVersionUID to suppress a javac warning. 603 */ 604 private static final long serialVersionUID = 6138294804551838833L; 605 606 /** Thread this worker is running in. Null if factory fails. */ 607 @SuppressWarnings("serial") // Not statically typed as Serializable 608 final Thread thread; 609 /** Initial task to run. Possibly null. */ 610 @SuppressWarnings("serial") // Not statically typed as Serializable 611 Runnable firstTask; 612 /** Per-thread task counter */ 613 volatile long completedTasks; 614 615 // TODO: switch to AbstractQueuedLongSynchronizer and move 616 // completedTasks into the lock word. 617 618 /** 619 * Creates with given first task and thread from ThreadFactory. 620 * @param firstTask the first task (null if none) 621 */ 622 Worker(Runnable firstTask) { 623 setState(-1); // inhibit interrupts until runWorker 624 this.firstTask = firstTask; 625 this.thread = getThreadFactory().newThread(this); 626 } 627 628 /** Delegates main run loop to outer runWorker. */ 629 public void run() { 630 runWorker(this); |