Sign in to follow this  
ahung89

[java] concurrency question

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Threads represent multiple processes but they still run in a sequential order like multiple OS threads on a single core machine.

A Thread does not necessarily run to completion but might be given a time slice to execute, is paused/suspended while another Thread is running and be resumed again. If you call Thread.yield() you can suspend the thread yourself in order to give the JVM a hint that it should resume another thread. The JVM may, however, choose to resume the same thread, since that's not defined in the specification.

Hope that makes it clearer a bit.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this