Massive Scheduling with ScheduledExecutorService in Java
If you need to do certain things at certain points of time or intervals, you need scheduling capability. Don’t confuse the scheduling with multi-threading even though you can use multi-threading for scheduling but they are not equivalent. You can use single thread for many different tasks. Simply put, threads are executors, and tasks are jobs to be done by executors.
Instead of writing your own code, you can leverage existing APIs. Most people consider Quartz scheduling API which has many features. I don’t have experience with the API even though I heard others talking about it.
In most cases, I prefer Java standard APIs over other APIs if it can get my work done because I don’t need to download and manage extra libraries. Incorporating extra libraries is not a big deal mostly, but could sometimes turn into ugly problems with incompatible dependency among them. Also if you can avoid extra work, why not?
To my curiosity, I started to search for a solution in Java standard APIs and got the ScheduledExecutorService introduced in Java 1.5. It’s well documented with a nice sample code showing how to use the API. I was still not sure the scalability of the API. In other words, how many tasks can be scheduled? This capability is not demoed in the sample code provided.
For that, I simply modified the sample as listed in the end. Well above my expectation, the ScheduledExecutorService can actually schedule huge number of tasks, each of which is executed at a different interval, with one thread. I tried both 2,000 and 100,000 repetitive tasks. After the latter test with 100,000 tasks is done successfully, I decided that is probably more than enough for any real projects.
I think ScheduledExecutorService can handle “unlimited” tasks given that you have enough memory and each task can be done quickly enough before next execution (of this task or other task) comes. For my test, the 2,000 tasks used 1,006,744 bytes of memory, and the 100,000 tasks 16,509,080 bytes. Allocated to each task, it’s about 500 and 165 bytes of memory respectively. Your miles may vary, but should be close.
In conclusion, the ScheduledExecutorService API coming with the Java standard library is a solid implementation with massive scheduling capabilities. It should satisfy most of your scheduling needs.
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
// modified from the sample code in Java API doc as listed below
// http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html
class BeeperControl
{
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public static void main(String[] args)
{
new BeeperControl().beepForAnHour();
}
public void beepForAnHour()
{
System.out.println(Runtime.getRuntime().freeMemory());
final ScheduledFuture<?>[] beeperHandles = new ScheduledFuture<?>[2000];
for(int i=0; i<beeperHandles.length; i++)
{
beeperHandles[i] = scheduler.scheduleAtFixedRate(new Beeper(i), 10, i+1, SECONDS);
}
scheduler.schedule(new Runnable()
{
public void run() { for(ScheduledFuture sf: beeperHandles) { sf.cancel(true); } }
}, 60 * 60, SECONDS);
System.out.println(Runtime.getRuntime().freeMemory());
}
static class Beeper implements Runnable
{
private int id;
public Beeper(int id)
{
this.id = id;
}
public void run()
{
System.out.println("beep by " + id);
}
};
}

Massive Scheduling with ScheduledExecutorService in Java http://t.co/mn50vxm3 via @sjin2008
Massive Scheduling with ScheduledExecutorService in Java (DoubleCloud) http://t.co/FJnwQh1f