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); }
protected void task(T command) { if (requestedShutdown) { logAddOnShutdown(); } //The shutdown process could finish right after the above check: shutdownNow can drain the remaining tasks tasks.add(command); //cache locally the state to avoid multiple volatile loads final int state = stateUpdater.get(this); if (state != STATE_RUNNING) { onAddedTaskIfNotRunning(state); } }
/** * It will shutdown and wait 30 seconds for timeout. */ public void shutdown() { shutdown(30, TimeUnit.SECONDS); }
/** * It will shutdown and wait 30 seconds for timeout. */ public void shutdown() { shutdown(30, TimeUnit.SECONDS); }
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); }
protected void task(T command) { if (requestedShutdown) { logAddOnShutdown(); } //The shutdown process could finish right after the above check: shutdownNow can drain the remaining tasks tasks.add(command); //cache locally the state to avoid multiple volatile loads final int state = stateUpdater.get(this); if (state != STATE_RUNNING) { onAddedTaskIfNotRunning(state); } }
/** * It will shutdown and wait 30 seconds for timeout. */ public void shutdown() { shutdown(30, TimeUnit.SECONDS); }
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); }
protected void task(T command) { if (requestedShutdown) { logAddOnShutdown(); } //The shutdown process could finish right after the above check: shutdownNow can drain the remaining tasks tasks.add(command); //cache locally the state to avoid multiple volatile loads final int state = stateUpdater.get(this); if (state != STATE_RUNNING) { onAddedTaskIfNotRunning(state); } }
/** * It will shutdown and wait 30 seconds for timeout. */ public void shutdown() { shutdown(30, TimeUnit.SECONDS); }
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); }
protected void task(T command) { if (requestedShutdown) { logAddOnShutdown(); } //The shutdown process could finish right after the above check: shutdownNow can drain the remaining tasks tasks.add(command); //cache locally the state to avoid multiple volatile loads final int state = stateUpdater.get(this); if (state != STATE_RUNNING) { onAddedTaskIfNotRunning(state); } }
/** * It will shutdown and wait 30 seconds for timeout. */ public void shutdown() { shutdown(30, TimeUnit.SECONDS); }
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); }
protected void task(T command) { if (requestedShutdown) { logAddOnShutdown(); } //The shutdown process could finish right after the above check: shutdownNow can drain the remaining tasks tasks.add(command); //cache locally the state to avoid multiple volatile loads final int state = stateUpdater.get(this); if (state != STATE_RUNNING) { onAddedTaskIfNotRunning(state); } }