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

Implement PausableScheduledThreadPool #61

Merged
merged 8 commits into from
Feb 3, 2021
Merged
Changes from 1 commit
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
Expand Up @@ -32,7 +32,7 @@ public class PausableScheduledThreadPool extends ScheduledThreadPoolExecutor {
private static final Logger LOG = Log.logger(
PausableScheduledThreadPool.class);

private boolean paused = false;
private volatile boolean paused = false;

public PausableScheduledThreadPool(int corePoolSize) {
super(corePoolSize);
Expand All @@ -43,20 +43,22 @@ public PausableScheduledThreadPool(int corePoolSize,
super(corePoolSize, factory);
}

public synchronized void pauseSchedule() {
public void pauseSchedule() {
this.paused = true;
LOG.info("PausableScheduledThreadPool was paused");
}

public synchronized void resumeSchedule() {
public void resumeSchedule() {
this.paused = false;
this.notifyAll();
synchronized (this) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this.paused = false; into synchronized block to avoid race between resumeSchedule and resumeSchedule

this.notifyAll();
}
LOG.info("PausableScheduledThreadPool was resumed");
}

@Override
protected synchronized void beforeExecute(Thread t, Runnable r) {
if (this.paused) {
protected void beforeExecute(Thread t, Runnable r) {
synchronized (this) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep all paused writing synchronized

while (this.paused) {
try {
this.wait();
Expand All @@ -69,15 +71,15 @@ protected synchronized void beforeExecute(Thread t, Runnable r) {
}

@Override
public synchronized void shutdown() {
public void shutdown() {
if (this.paused) {
this.resumeSchedule();
}
super.shutdown();
}

@Override
public synchronized List<Runnable> shutdownNow() {
public List<Runnable> shutdownNow() {
if (this.paused) {
this.resumeSchedule();
}
Expand Down