Results 1 to 5 of 5
  1. #1
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default 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.

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    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().

  3. #3
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default

    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 ;)

  4. #4
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    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?

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    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)

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. JSP Question
    By maheshkumarjava in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-29-2008, 11:51 AM
  3. JNI question
    By javaplus in forum New To Java
    Replies: 0
    Last Post: 12-24-2007, 11:18 AM
  4. Explanation bout threading and concurrency?
    By cruxblack in forum New To Java
    Replies: 1
    Last Post: 08-10-2007, 11:33 AM
  5. question about rmi
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •