Search code examples
javaconcurrencyjava.util.concurrent

What do tasks submitted to ScheduledExecutorService do while "waiting" to run


If I have something like this

scheduledExecutorService.schedule(task,60, TimeUnit.SECONDS);

What happens to "task" while that 60 seconds is not up yet. Does it consume resources? Consequently, the next question and my specific use case is what if i have a lot of "tasks" to schedule, would that be memory/resource efficient? My tasks are short-lived but i'm concerned about the implications of scheduling hundreds of tasks to the scheduledExecutorService.


Solution

  • What happens to "task" while that 60 seconds is not up yet. Does it consume resources?

    It waits in a PriorityQueue, so yes it use some resources.

    Consequently, the next question and my specific use case is what if i have a lot of "tasks" to schedule, would that be memory/resource efficient?

    Not much worse than storing them in a Queue.

    My tasks are short-lived but i'm concerned about the implications of scheduling hundreds of tasks to the scheduledExecutorService.

    Say each task uses 1 KB, you will be wasting 100 KB. Unlikely to be a problem.

    Say each task uses 1 GB, then you might have a problem. I suggest you do it another way.