Whenever your application has several things to do, you need to figure out how to schedule those things. This episode explains a common technique called a round robin that gives everything a fair share.

Let’s say that you have multiple tasks you want to manage and track yourself in your application. What kind of tasks?

  • Your application might allow users to place markers on topics the user find interesting and notify the user whenever any changes occur to those topics.
  • Your application might monitor the performance of some object or process and notify the user of any irregularities. the user could select multiple things to monitor.
  • Your application has it’s own tasks it needs to do periodically. Maybe it needs to check for updates so the user is automatically running the latest version.

Whatever you need to do, if you have a list of these tasks and you need to make progress on all of them, then a round robin is a good technique. It’s a simple approach, just start with the first item, advance to the next either when the first task is done or after some predetermined time or amount of progress. Then move to the next item and do the same thing. After all the tasks have been given a turn, go back to the first task.

You need to manage the switching of tasks yourself. And this needs to be done from within the task. For a long running task, just make sure to check every now and then if a switch should be made. Listen to the full episode for more or read the full transcript below.

Transcript

It seems we can never escape having too much to do. This problem follows us even into the virtual world. Let’s think for a moment about how we handle this in the real world and then I’ll explain how one of those systems works in programming. Here’s 6 different approaches that I thought of. I’m sure there are more and definitely there are variations on each of these. Anyway, here’s 6 that I just thought of:

#1 Only the strong survive. We’re not the only ones with problems figuring out what to do next or who to take care of first. Wildlife can sometimes be harsh but with good reason. If a mother bird comes back to a nest of three chicks, how does the mother decide who to feed? The mother will feed all the chicks that have a good chance of survival. If one of the chicks can’t get up though even if the only thing wrong with it is that it just needs some more food, then it’s days are numbered. The biggest and strongest chick usually gets fed first. Is this fair? No. But it’s not designed to be fair. It’s designed to use scarce resources on the ones best able to put that food to good use.

#2 Focus on a task until it’s done. For all the good things people say about multitasking and all the books written about the topic, it’s still less efficient than just working on one thing at a time. Have you ever been busy with a task and get interrupted by somebody? What did you do when you returned to your task? Most of us would say, “Okay, where was I?” then spend another 15 minutes getting back in the groove. That’s what I mean when I say multitasking is less efficient. And for some tasks that require a lot of concentration, continuous interruptions will mean that the task can never complete.

#3 Do the important stuff first. If you know that you have an important speech to give next week, then today may not be the best time to hand wash your car.

#4 Work on whichever task best fits your current situation. Maybe you’re tired. Recognize this fact and choose to do things that don’t need a lot of mental effort. Deciding to read a technical book at this time is almost the same thing as just going to sleep, although probably not in a very comfortable position.

#5 Make a list and just start working. If you spend too long on any item, stop and move on to the next. You can always come back again and continue where you left off. If you’re like me, you have a ton of things you want to be doing and some of them are just collecting dust. Taking this approach will make sure that nothing gets ignored for too long.

And #6 Just pick something at random and work on it until you get bored or distracted. We’ve all done this and I can tell you definitely that nothing gets done. We seem busy though.

Depending on what popular time management book currently has your attention, you my find it recommends either #2, #3, or #4. Why would I devote this episode to #5 then? Make a list and just start working on it. Well, I had to start somewhere and this is an important topic. This approach is commonly called a round robin.

I’ll explain more right after this message from our sponsor.

( Message from Sponsor )

I think one reason why the round robin approach is good for programming is because if you go to all the trouble to create a task in your application. And I’m not talking about a time management application that’s designed to track user tasks. I’m talking about some calculation you need to perform. Or some activity you need to perform. If you go to the trouble of setting up a task like this, then why would you want to just let it sit there without any progress? This is not like the messy real world where we have dreams of becoming an expert at something but abandon the effort after a while.

In the software world, when you have something that needs to be done, then you want to make sure that you put a process in place that gives that task a chance to continue. Otherwise, why bother in the first place? If you know that you’re not going to finish something, then it’s a lot more efficient to never begin.

Based on this, everything that’s in your application is put there for a valid reason and is something that you want to either complete if it’s the type of task that has a definite end, or to keep running if it’s the type of task that’s meant to continuously do something.

And if there’s one thing that computers are good at, it’s following instructions. They won’t forget. Well, I suppose they could forget if a data file gets corrupt. Or if a stray cosmic ray strikes the precise location of a bit in memory causing it to change. Hey, I’m not just making that part up. If I was writing software to run on the space station, then I’d definitely be putting systems in place to handle radiation.

The round robin approach gets its name from a common technique where everybody gets a turn. Think back to your school days when the teacher would ask everybody to read a paragraph out loud. This is round robin reading.

Most of the time when programming, this topic comes up when you have multiple threads and the operating system needs to schedule the threads. Assuming a thread is awake and doing work and not waiting on some other task, then the operating system will usually give each thread a certain amount of time to run before yanking control away from the thread and letting another thread have a turn.

Beyond this, though, there will be times when you need to manage several things. Maybe these are not suitable for separate threads of execution that the operating system can manage. These are things that you want to manage yourself.

If you find yourself in this situation, then a round robin is a good choice. As long as you work on each task in equal amounts, move on to the next, and then go back to the first when you’ve went through them all, then you’ll know that progress is being made on all of them and that you’re not forgetting any of them. In other words, none of the tasks is starving.

If you need to pause for a while and start again later, then you’ll need to decide where to resume. I say you need to decide, because there’s no right answer for all cases. Maybe you want to always start again at the beginning. That’s fine. Just be aware that if you have frequent pausing and resuming compared to the amount of time it takes to make a full pass through the tasks, that this can lead to a form of starvation.

If that’s a problem, then the solution is to maintain a variable that records your place in the list when pausing. Then when you resume, instead of starting at the beginning of the list, first navigate to the last item that was being worked on before the pause. Then just continue where you left off.

This assumes that your tasks have some kind of order to them. If when you resume, you find that the tasks are in a different order, then maybe it makes more sense to just start at the beginning each time.

You’ll also have to decide how to manage the amount of time to spend on each task. As a normal process, your application won’t have the power of the operating system to rip control away from one of your tasks and start another one. You need to manage this yourself from within each task.

A good way to do this is to set a timer that will alert your application at specific intervals and all you need to do is set a variable to true so your code will know that the timer just ran.

Then in your tasks, make sure to periodically check this value to see of it changed to true. And if so, then wrap up what your task was doing, and get ready to switch to the next task.