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.

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

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);
    }
  };
}

This entry was posted in Software Development and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted January 14, 2013 at 2:28 am | Permalink

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

  2. Posted January 14, 2013 at 3:30 am | Permalink

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

One Trackback

  • […] single thread for many different tasks. Simply put, threads are executors, and tasks are jobs […]Massive Scheduling with ScheduledExecutorService in Java originally appeared on DoubleCloud by Steve Jin, author of VMware VI and vSphere SDK (Prentice […]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  • NEED HELP?


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.