Hey all. I've started a new book - "Killer Java Game Programming." I've been reading the first chapter which talks about the animation framework. There's one concept that I'm confused about. I just recently learned about concurrency and what I understand is that threads are different processes that are running simultaneously. But one of the features of the code in the animation framework, as stated by the author, has "Thread.yield() utilized to give other threads a chance to execute if the animation loop has not slept for a while." Why would it have to yield if the other threads are already running at the same time? Doesn't make any sense to me. I'd really appreciate if someone could clarify this for me.
It's about how the operating system shares out the available processors among the running threads.
A typical home computer has 1 or 2 processors (and a typical server might have, say, 4 or 8 processors). But on either, there could be hundreds or even thousands of threads "running" at any given moment. So the operating system has to juggle these threads among the available processors. Usually, the operating system makes decisions as to when this "juggling" happens, and will pick "strategic moments" to do so: for example, if the thread currently running on a processor needs to wait for data to come from a disk/the network, that's a strategic moment to hand the CPU over to another thread while it waits. In the worst case scenario, where a thread never reaches a "strategic point", then the OS will give a thread a certain time limit before it effectively forces it off the CPU and "schedules in" another thread on that CPU.
Now, what Thread.yield() effectively allows you to do is communicate with the operating system and say "I've reached a strategic moment for swapping me for another thread". It's a way for your thread to "be nice" to other threads that are trying to run.
Unfortunately, there are also some problems with Thread.yield(): in particular, the behaviour of Thread.yield() is highly dependent on the OS/version in use. For more information, see the article I have written on the behaviour of Thread.yield().
Although computers can run multiple threads it doesn't actually mean that they are technically running at the same time. They just usually get a priority assigned to them and then carried out in a new order of execution. Problem is, if a High Priority thread is running a loop with no I/O and never sleep/s yield/s wait/s or perform/s then no other thread will ever execute.
There's a really good article at Thread use in Java - JavaWorld
hope it helps ;)
wow - i totally didn't know that. thanks to both of you for the explanations.
so just to clarify - there is actually only ONE thread running at any given time? and the other threads run during the idle time?
There is only ever one thread actually running per CPU (core) (read about HyperThreading for an exception to this rule). So, for example, if you have a Dual Core machine, then up to two threads can physically be running simultaneously.
By the way, the previous poster's comment about thread priorities is more-or-less true. But:
- operating systems generally have a contingency plan for the possibility that a thread might be "starved" of CPU -- even Windows isn't quiiite as dumb as you'd think
- thread priorities don't necessarily do what you think they do (I've also written articles on thread priorities in Java and thread scheduling in general which go into these matters in more detail)