Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adriansr/72d5fce8bc60b4a5018d5cf07b2e6827 to your computer and use it in GitHub Desktop.
Save adriansr/72d5fce8bc60b4a5018d5cf07b2e6827 to your computer and use it in GitHub Desktop.
class LoggingScheduledThreadPoolExecutor(nthreads: Int)
extends ScheduledThreadPoolExecutor(nthreads) {
private val log = Logger(LoggerFactory.getLogger(classOf[LoggingScheduledThreadPoolExecutor]))
class CustomTask[T](runnable: AnyRef, task: RunnableScheduledFuture[T])
extends RunnableScheduledFuture[T] {
val creationTime = System.nanoTime()
override def isCancelled: Boolean = task.isCancelled
override def isPeriodic: Boolean = task.isPeriodic
override def getDelay(unit: TimeUnit): Long = task.getDelay(unit)
override def run(): Unit = {
val startTime = System.nanoTime()
val timeWaiting = (startTime - creationTime) / 1000000
log debug s"Starting task $task after $timeWaiting ms in queue"
task.run()
val tookTime = (System.nanoTime() - startTime) / 1000000
log debug s"Finished task $task took $tookTime ms"
}
override def compareTo(o: Delayed): Int = task.compareTo(o)
override def get(): T = task.get()
override def get(timeout: Long,
unit: TimeUnit): T = task.get(timeout,unit)
override def cancel(mayInterruptIfRunning: Boolean): Boolean = task.cancel(mayInterruptIfRunning)
override def isDone: Boolean = task.isDone
}
override def decorateTask[V](runnable: Runnable,
task: RunnableScheduledFuture[V]): RunnableScheduledFuture[V] = {
new CustomTask(runnable,task)
}
override def decorateTask[V](callable: Callable[V],
task: RunnableScheduledFuture[V]): RunnableScheduledFuture[V] = {
new CustomTask(callable,task)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment