Skip to content

Commit

Permalink
scheduler will now wait for threads on shutdown (#1137)
Browse files Browse the repository at this point in the history
Updated the scheduler helper to have the shutdown method block
until the threads stop. This makes it easier for callers to
ensure they are not competing with in-progress tasks while
performing other shutdown work.
  • Loading branch information
brharrington authored Jun 17, 2024
1 parent 55c82c6 commit 7ccf552
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 Netflix, Inc.
* Copyright 2014-2024 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -170,15 +170,28 @@ public ScheduledFuture<?> schedule(Options options, Runnable task) {

/**
* Shutdown and cleanup resources associated with the scheduler. All threads will be
* interrupted, but this method does not block for them to all finish execution.
* interrupted and this method will block until they are shutdown.
*/
public void shutdown() {
lock.lock();
try {
shutdown = true;

// Interrupt threads to shutdown
for (Thread thread : threads) {
if (thread != null && thread.isAlive()) {
thread.interrupt();
}
}

// Wait for all threads to complete
for (int i = 0; i < threads.length; ++i) {
if (threads[i] != null && threads[i].isAlive()) {
threads[i].interrupt();
if (threads[i] != null) {
try {
threads[i].join();
} catch (Exception e) {
LOGGER.debug("exception while shutting down thread {}", threads[i].getName(), e);
}
threads[i] = null;
}
}
Expand All @@ -188,6 +201,13 @@ public void shutdown() {
}

private void startThreads() {
// Normally when a thread exits, it will try to restart, if shutting down
// exit early before trying to get the lock.
if (shutdown) {
return;
}

// Start threads if setting up the scheduler or if a thread failed.
lock.lock();
try {
if (!shutdown) {
Expand Down Expand Up @@ -478,7 +498,7 @@ private final class Worker implements Runnable {
try {
// Note: do not use Thread.interrupted() because it will clear the interrupt
// status of the thread.
while (!Thread.currentThread().isInterrupted()) {
while (!shutdown && !Thread.currentThread().isInterrupted()) {
try {
DelayedTask task = queue.take();
stats.incrementActiveTaskCount();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 Netflix, Inc.
* Copyright 2014-2024 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 7ccf552

Please sign in to comment.