Skip to content

Commit

Permalink
Make sure the timer hack thread is only started once
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenni0451 committed Jan 8, 2025
1 parent 950f96f commit a78152f
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import lombok.experimental.UtilityClass;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

@UtilityClass
public class ThreadUtils {

private static final AtomicBoolean TIMER_HACK_THREAD_STARTED = new AtomicBoolean(false);
private static final long SLEEP_PRECISION = TimeUnit.MILLISECONDS.toNanos(2);
private static final long SPIN_YIELD_PRECISION = TimeUnit.MICROSECONDS.toNanos(2);

Expand All @@ -15,14 +17,16 @@ public class ThreadUtils {
* This increases the precision of {@link Thread#sleep(long)}.
*/
public static void startTimerHackThread() {
Thread thread = new Thread(() -> {
try {
while (true) Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException ignored) {
}
}, "TimerHackThread");
thread.setDaemon(true);
thread.start();
if (TIMER_HACK_THREAD_STARTED.compareAndSet(false, true)) {
Thread thread = new Thread(() -> {
try {
while (true) Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException ignored) {
}
}, "TimerHackThread");
thread.setDaemon(true);
thread.start();
}
}

/**
Expand Down

0 comments on commit a78152f

Please sign in to comment.