All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.teamscale.jacoco.agent.util.Timer Maven / Gradle / Ivy

Go to download

JVM profiler that simplifies various aspects around recording and uploading test coverage

There is a newer version: 34.0.2
Show newest version
/*-------------------------------------------------------------------------+
|                                                                          |
| Copyright (c) 2009-2018 CQSE GmbH                                        |
|                                                                          |
+-------------------------------------------------------------------------*/
package com.teamscale.jacoco.agent.util;

import java.time.Duration;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Triggers a callback in a regular interval. Note that the spawned threads are
 * Daemon threads, i.e. they will not prevent the JVM from shutting down.
 * 

* The timer will abort if the given {@link #runnable} ever throws an exception. */ public class Timer { /** Runs the job on a background daemon thread. */ private final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, runnable -> { Thread thread = Executors.defaultThreadFactory().newThread(runnable); thread.setDaemon(true); return thread; }); /** The currently running job or null. */ private ScheduledFuture job = null; /** The job to execute periodically. */ private final Runnable runnable; /** Duration between two job executions. */ private final Duration duration; /** Constructor. */ public Timer(Runnable runnable, Duration duration) { this.runnable = runnable; this.duration = duration; } /** Starts the regular job. */ public synchronized void start() { if (job != null) { return; } job = executor.scheduleAtFixedRate(runnable, duration.toMinutes(), duration.toMinutes(), TimeUnit.MINUTES); } /** Stops the regular job, possibly aborting it. */ public synchronized void stop() { job.cancel(false); job = null; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy