Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scheduler will now wait for threads on shutdown #1137

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading