ScheduledExecutorServices

Scheduled Executor Service and Quota time chart

I briefly touched on this in my first post, but I find that there are quite a few things which I aim to carry out (at least / at most) N times per time interval T. This is described by John Sonmez in Soft Skills as a quota. You may have been able to infer this from the frequency of posts here – I’ve been aiming to write at least 1 blog post per week.

On another note, scheduled executor services are a part of the Java concurrency libraries (introduced in Java 5). They allow clients to submit one-shot tasks wrapped in callables (possibly with some delay before execution), as well as tasks with a fixed rate (more similar to a quota, though not quite – mainly because quotas are concerned with getting things done, while executor services are concerned with enabling tasks to run; also because a scheduled executor service won’t start concurrent tasks). Clients can also specify tasks with a fixed delay; this differs from a fixed rate in that the countdown to the next execution starts after the current task has completed.

If one assumes that the tasks complete relatively quickly, then quotas are, in a way, less restrictive than scheduled executor services; they give flexibility as to when in the time period T the N events occur. This is especially important for tasks that require 2-way synchronization (for me, that largely involves spending time with friends) – it would be even more so for barriers involving multiple people though I haven’t actually planned any such arrangements.

A downside of this is that it delays the decision of deciding when in each period T the events should be scheduled; it’s arguably simpler to schedule them at the same point in each period. Also, if one follows the letter of the quota, then this can lead to very uneven intervals between occurrences – for syncing up with friends, blog posts and quite a few other things, while this certainly isn’t bad it’s also less than ideal (imagine if I had a “weekly sync” with a friend, but actually spoke to them once every 2 weeks at 23.35 on Sunday for 20 minutes, and then again at 00.05 on Monday for 20 minutes). I find that a good way around this is to normally target the same point, but allow for flexibility; I’m not sure you can readily do this in ScheduledExecutorService (you’d have to cancel the old task and reschedule a new one with the correct delay, I think).

The diagram above more succinctly illustrates the difference between the timing semantics for the various things I’ve described. More often than not, at least for meeting up and/or syncing with people, the pattern is closer to the second or fourth above (i.e. with random variation, plus the occasional additional occurrence; perhaps the occasional missed occurrence as well, though I didn’t include that in the diagram).

Another way I’ve modeled this in the past is on the concept of a failure detector in distributed systems. Servers/subsystems can arrange periodic heartbeats, suspecting them of failure if a heartbeat is not received (and “un-suspecting” them if one is subsequently received). Though because of the aforementioned flexibility, a pattern that conforms to a quota could result in a heartbeat interval of 2T. I guess the idea I had previously was I didn’t want to lose contact with friends, bearing in mind that I was still in Imperial at that time and thus would quite naturally and easily meet many of my friends in lectures or in labs – on the other hand, I’m the only person from my class to go to Palantir (at least at this point). I find using a system based on quotas is certainly much easier for me to manage, as well.


Leave a Reply