Results 1 to 6 of 6

Thread: Differentiation between yield() and sleep()

  1. #1
    Join Date
    Dec 2009
    Posts
    67

    Differentiation between yield() and sleep()

    Hello friends,

    I want to know about the difference between yield() and sleep() methods of java. I wonder if you are able to descried these two java methods. Both yield() and sleep() seems very difficult to understand. I have referred various books but didn't get enough information about yield() and sleep(). Please help me to understand the differentiation between yield() and sleep().

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Differentiation between yield() and sleep()

    yield() method of java used to call the Windows API call Sleep(0). all runnable threads of the same priority will get a chance to run before the yielded thread is next given CPU time. This behavior of yield() method of java is a quit varies from a non-zero sleep where the sleeping thread generally loses 1 quantum value. This method has special effect which clears the current thread's quantum and putting it to the end of the queue for its priority level.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Differentiation between yield() and sleep()

    The sleep() method of java is important for aware about current thread. The thread of class may not sleep for the specified time and the sleep can be interrupted (sometimes useful for implementing a cancellation function). While sleeping, the thread get synchronization locks it has acquired. calling sleep() with certain values can have some subtle, global effects on the OS, other threads and processes running on the system can have subtle effects on the observed sleep duration.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Differentiation between yield() and sleep()

    Hi,

    It is important in a cooperative multithread environment, since without yield(), one thread can eat up all of the CPU. yield() method of java language informs the JVM (java virtual machine) thread Scheduler that it is valid to give other time slices of threads. JVM uses this call to activate another thread of the same thread priority. In a good preemptive environment of multithread yield() method is a no-op. See below example of sleep method:
    Thread.sleep(2500L);

    Thread.sleep(2050L, 550000L);

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Differentiation between yield() and sleep()

    Calling sleep() method of java causes the thread to suspend for at least specified milliseconds. While the calling of yield() method of java causes thread of class to rejoin the queue. The platform of Java programming uses the multithread feature of the host Operating system for to implement multithreading. This means multithreaded Java language programs can exhibit platform-dependent behavior.

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Differentiation between yield() and sleep()

    Neither yield method nor yield method change anything about synchronization locks status. A yielded thread will not get another slice of CPU until all other threads have had a slice of CPU. If your thread has a lock, and you call sleep(1000), then at least a second will elapse before your thread wakes up. When it wakes up it may decide to release the lock or it may hold on to it longer. sleep will pause the current thread for the specified number of milliseconds.

Similar Threads

  1. Replies: 4
    Last Post: 06-04-2010, 12:41 AM
  2. Replies: 5
    Last Post: 27-01-2010, 04:57 PM
  3. Differentiation between WAN and LAN
    By Dolsy_bendal in forum Networking & Security
    Replies: 4
    Last Post: 30-11-2009, 10:46 AM
  4. Tricks for Improving the Yield of my pc?
    By ameer123 in forum Hardware Peripherals
    Replies: 2
    Last Post: 22-10-2008, 02:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,718,050,527.30941 seconds with 17 queries