Repository: jenkinsci/jenkins
File: core/src/main/java/hudson/model/AsyncPeriodicWork.java (Java)

Code before:
                }
                return;
            }
            thread = new Thread(() -> {
                logger.log(Level.FINE, "Started {0}", name);
                long startTime = System.currentTimeMillis();
                long stopTime;

                LazyTaskListener l = new LazyTaskListener(this::createListener, String.format("Started at %tc", new Date(startTime)));
                try {
                    try (ACLContext ctx = ACL.as2(ACL.SYSTEM2)) {
                        execute(l);
                    }
                } catch (IOException e) {
                    Functions.printStackTrace(e, l.fatalError(e.getMessage()));
                } catch (InterruptedException e) {
                    Functions.printStackTrace(e, l.fatalError("aborted"));
                } finally {
                    stopTime = System.currentTimeMillis();
                    l.close(String.format("Finished at %tc. %dms", new Date(stopTime), stopTime - startTime));
                }

                logger.log(Level.FINE, "Finished {0}. {1,number} ms",
                        new Object[]{name, stopTime - startTime});
                thread = null;
                if (queueIfAlreadyRunning() && pending.getAndSet(false)) {
                    logger.log(this.getSlowLoggingLevel(), "An execution of {0} was requested while it was running, scheduling another run now", name);
                    doRun();
                }
            }, name + " thread");
            thread.start();
        } catch (Throwable t) {
            LogRecord lr = new LogRecord(this.getErrorLoggingLevel(), "{0} thread failed with error");
            lr.setThrown(t);
            lr.setParameters(new Object[]{name});
            logger.log(lr);
        }
    }

    static final class LazyTaskListener implements TaskListener {

        private final Supplier<StreamTaskListener> supplier;
        private final String openingMessage;
        private StreamTaskListener delegate;

        LazyTaskListener(Supplier<StreamTaskListener> supplier, String openingMessage) {
            this.supplier = supplier;
            this.openingMessage = openingMessage;
        }

        @Override

Review comment (suggestion):
or
```suggestion
                if (pending.getAndSet(false)) {
```
should suffice

Code after:
                }
                return;
            }
            thread = new Thread(() -> {
                logger.log(Level.FINE, "Started {0}", name);
                long startTime = System.currentTimeMillis();
                long stopTime;

                LazyTaskListener l = new LazyTaskListener(this::createListener, String.format("Started at %tc", new Date(startTime)));
                try {
                    try (ACLContext ctx = ACL.as2(ACL.SYSTEM2)) {
                        execute(l);
                    }
                } catch (IOException e) {
                    Functions.printStackTrace(e, l.fatalError(e.getMessage()));
                } catch (InterruptedException e) {
                    Functions.printStackTrace(e, l.fatalError("aborted"));
                } finally {
                    stopTime = System.currentTimeMillis();
                    l.close(String.format("Finished at %tc. %dms", new Date(stopTime), stopTime - startTime));
                }

                logger.log(Level.FINE, "Finished {0}. {1,number} ms",
                        new Object[]{name, stopTime - startTime});
                thread = null;
                if (pending.getAndSet(false)) {
                    logger.log(this.getSlowLoggingLevel(), "An execution of {0} was requested while it was running, scheduling another run now", name);
                    doRun();
                }
            }, name + " thread");
            thread.start();
        } catch (Throwable t) {
            LogRecord lr = new LogRecord(this.getErrorLoggingLevel(), "{0} thread failed with error");
            lr.setThrown(t);
            lr.setParameters(new Object[]{name});
            logger.log(lr);
        }
    }

    static final class LazyTaskListener implements TaskListener {

        private final Supplier<StreamTaskListener> supplier;
        private final String openingMessage;
        private StreamTaskListener delegate;

        LazyTaskListener(Supplier<StreamTaskListener> supplier, String openingMessage) {
            this.supplier = supplier;
            this.openingMessage = openingMessage;
        }

        @Override