private void executePendingTasks() { do { //if there is no thread active and is not already dead then we run if (stateUpdater.compareAndSet(this, STATE_NOT_RUNNING, STATE_RUNNING)) { enter(); try { T task; //while the queue is not empty we process in order: //if requestedForcedShutdown==true than no new tasks will be drained from the tasks q. while (!requestedForcedShutdown && (task = tasks.poll()) != null) { doTask(task); } } finally { leave(); //set state back to not running if possible: shutdownNow could be called by doTask(task). //If a shutdown has happened there is no need to continue polling tasks if (!stateUpdater.compareAndSet(this, STATE_RUNNING, STATE_NOT_RUNNING)) { return; } } } else { return; } //we loop again based on tasks not being empty. Otherwise there is a window where the state is running, //but poll() has returned null, so a submitting thread will believe that it does not need re-execute. //this check fixes the issue } while (!tasks.isEmpty() && !requestedShutdown); }
private void executePendingTasks() { do { //if there is no thread active and is not already dead then we run if (stateUpdater.compareAndSet(this, STATE_NOT_RUNNING, STATE_RUNNING)) { enter(); try { T task; //while the queue is not empty we process in order: //if requestedForcedShutdown==true than no new tasks will be drained from the tasks q. while (!requestedForcedShutdown && (task = tasks.poll()) != null) { doTask(task); } } finally { leave(); //set state back to not running if possible: shutdownNow could be called by doTask(task). //If a shutdown has happened there is no need to continue polling tasks if (!stateUpdater.compareAndSet(this, STATE_RUNNING, STATE_NOT_RUNNING)) { return; } } } else { return; } //we loop again based on tasks not being empty. Otherwise there is a window where the state is running, //but poll() has returned null, so a submitting thread will believe that it does not need re-execute. //this check fixes the issue } while (!tasks.isEmpty() && !requestedShutdown); }
private void executePendingTasks() { do { //if there is no thread active and is not already dead then we run if (stateUpdater.compareAndSet(this, STATE_NOT_RUNNING, STATE_RUNNING)) { enter(); try { T task; //while the queue is not empty we process in order: //if requestedForcedShutdown==true than no new tasks will be drained from the tasks q. while (!requestedForcedShutdown && (task = tasks.poll()) != null) { doTask(task); } } finally { leave(); //set state back to not running if possible: shutdownNow could be called by doTask(task). //If a shutdown has happened there is no need to continue polling tasks if (!stateUpdater.compareAndSet(this, STATE_RUNNING, STATE_NOT_RUNNING)) { return; } } } else { return; } //we loop again based on tasks not being empty. Otherwise there is a window where the state is running, //but poll() has returned null, so a submitting thread will believe that it does not need re-execute. //this check fixes the issue } while (!tasks.isEmpty() && !requestedShutdown); }
private void executePendingTasks() { do { //if there is no thread active and is not already dead then we run if (stateUpdater.compareAndSet(this, STATE_NOT_RUNNING, STATE_RUNNING)) { enter(); try { T task; //while the queue is not empty we process in order: //if requestedForcedShutdown==true than no new tasks will be drained from the tasks q. while (!requestedForcedShutdown && (task = tasks.poll()) != null) { doTask(task); } } finally { leave(); //set state back to not running if possible: shutdownNow could be called by doTask(task). //If a shutdown has happened there is no need to continue polling tasks if (!stateUpdater.compareAndSet(this, STATE_RUNNING, STATE_NOT_RUNNING)) { return; } } } else { return; } //we loop again based on tasks not being empty. Otherwise there is a window where the state is running, //but poll() has returned null, so a submitting thread will believe that it does not need re-execute. //this check fixes the issue } while (!tasks.isEmpty() && !requestedShutdown); }
private void executePendingTasks() { do { //if there is no thread active and is not already dead then we run if (stateUpdater.compareAndSet(this, STATE_NOT_RUNNING, STATE_RUNNING)) { enter(); try { T task; //while the queue is not empty we process in order: //if requestedForcedShutdown==true than no new tasks will be drained from the tasks q. while (!requestedForcedShutdown && (task = tasks.poll()) != null) { doTask(task); } } finally { leave(); //set state back to not running if possible: shutdownNow could be called by doTask(task). //If a shutdown has happened there is no need to continue polling tasks if (!stateUpdater.compareAndSet(this, STATE_RUNNING, STATE_NOT_RUNNING)) { return; } } } else { return; } //we loop again based on tasks not being empty. Otherwise there is a window where the state is running, //but poll() has returned null, so a submitting thread will believe that it does not need re-execute. //this check fixes the issue } while (!tasks.isEmpty() && !requestedShutdown); }
private void executePendingTasks() { do { //if there is no thread active and is not already dead then we run if (stateUpdater.compareAndSet(this, STATE_NOT_RUNNING, STATE_RUNNING)) { enter(); try { T task; //while the queue is not empty we process in order: //if requestedForcedShutdown==true than no new tasks will be drained from the tasks q. while (!requestedForcedShutdown && (task = tasks.poll()) != null) { doTask(task); } } finally { leave(); //set state back to not running if possible: shutdownNow could be called by doTask(task). //If a shutdown has happened there is no need to continue polling tasks if (!stateUpdater.compareAndSet(this, STATE_RUNNING, STATE_NOT_RUNNING)) { return; } } } else { return; } //we loop again based on tasks not being empty. Otherwise there is a window where the state is running, //but poll() has returned null, so a submitting thread will believe that it does not need re-execute. //this check fixes the issue } while (!tasks.isEmpty() && !requestedShutdown); }
private void executePendingTasks() { do { //if there is no thread active and is not already dead then we run if (stateUpdater.compareAndSet(this, STATE_NOT_RUNNING, STATE_RUNNING)) { enter(); try { T task; //while the queue is not empty we process in order: //if requestedForcedShutdown==true than no new tasks will be drained from the tasks q. while (!requestedForcedShutdown && (task = tasks.poll()) != null) { doTask(task); } } finally { leave(); //set state back to not running if possible: shutdownNow could be called by doTask(task). //If a shutdown has happened there is no need to continue polling tasks if (!stateUpdater.compareAndSet(this, STATE_RUNNING, STATE_NOT_RUNNING)) { return; } } } else { return; } //we loop again based on tasks not being empty. Otherwise there is a window where the state is running, //but poll() has returned null, so a submitting thread will believe that it does not need re-execute. //this check fixes the issue } while (!tasks.isEmpty() && !requestedShutdown); }