Skip to content
Open
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
Expand Up @@ -9,18 +9,27 @@
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

@SuppressWarnings("unused") @Getter
public class CustomTick {

private static final List<CustomTick> registeredTickEvents = new ArrayList<>();
private static final AtomicInteger threadCounter = new AtomicInteger();
private static final ThreadFactory threadFactory = runnable -> {
Thread thread = new Thread(runnable,"TIL-CustomTick-"+threadCounter.incrementAndGet());
// Custom ticks live for the process lifetime and must not prevent the JVM from shutting down.
thread.setDaemon(true);
return thread;
};

private static void addCustomTick(final CustomTick ticker) {
CommonEventsAPI api = EventHelper.getEventsAPI(false,false);
if(Objects.isNull(api) || isRegistered(ticker)) return;
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(() -> {
Executors.newScheduledThreadPool(1,threadFactory).scheduleAtFixedRate(() -> {
try {
api.postCustomTick(ticker);
} catch(Throwable t) {
Expand Down