When Not to Use Threads

When you look at any game, you'll notice a lot of things going on at once—enemies running around, doors opening, bullets flying, and so on. This leads some people to think, "I know, every enemy runs in its own thread," but this is not the case. Not only is this a waste of resources—running too many threads at once can drain the system—but this creates problems such as these:

  • An enemy could move in the middle of a draw operation, temporarily showing the enemy in two different places at once.
  • The time slices of each thread could be unbalanced, leading to jerky or inconsistent movement.
  • Synchronized code could lead to unneeded delays.

There are more efficient ways to have lots of things happening at the same time, which we'll get into in the next chapter,"2D Graphics and Animation."

Screenshot


   
Comments