91: Round Robin. Nobody Starves.

Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categories:

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,