Results 1 to 5 of 5
Thread: concurrency question
- 01-16-2009, 09:45 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 55
- Rep Power
- 0
concurrency question
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.
- 01-16-2009, 10:41 PM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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().Neil Coffey
Javamex - Java tutorials and performance info
- 01-16-2009, 10:45 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 43
- Rep Power
- 0
hi diggity,
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 ;)
- 01-17-2009, 02:54 AM #4
Member
- Join Date
- Dec 2008
- Posts
- 55
- Rep Power
- 0
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?
- 01-17-2009, 03:48 AM #5
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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)Neil Coffey
Javamex - Java tutorials and performance info
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
JSP Question
By maheshkumarjava in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-29-2008, 10:51 AM -
JNI question
By javaplus in forum New To JavaReplies: 0Last Post: 12-24-2007, 10:18 AM -
Explanation bout threading and concurrency?
By cruxblack in forum New To JavaReplies: 1Last Post: 08-10-2007, 10:33 AM -
question about rmi
By leonard in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks