aspose file tools*
The moose likes Threads and Synchronization and the fly likes Sleep and Yield state Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Sleep and Yield state" Watch "Sleep and Yield state" New topic
Author

Sleep and Yield state

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi i have the below clarification.

1> Difference's between sleep and yield state in Java ?
2> what is a context switch in java and what is it its impact in Java Threads.?

Thanks and Regards
Deepak Lal


When The Going Gets Tougher,The Tougher gets Going
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Deepak Lal wrote:Hi i have the below clarification.
1> Difference's between sleep and yield state in Java ?

Do you mean sleep() and yield() methods of Thread class? If so,
Calling sleep() from any thread halts the execution of the given thread for a specific period of time without losing the monitor of the thread. On the other hand invoking Thread.yield() causes the currently executing thread object to temporarily pause and allow other/this thread to execute. And both, sleep() and yield() don't release the acquired lock!

I couldn't understand your next question!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Sleep() means it will stop the execution of thread for a particular time.
yield() will change the state of thread form runing state to runnable state,so that the thread of same priority will get the chance to run.

Sleep throws Interrupted Exception i.e we have put call to sleep inside a try/catch block otherwise compiler error wil be throws.
Yield is a static method..

Both the sleep and yielddoesnot guarantee that they will do what they expect to do.
As mentioned by above that both donot even release the lock on the acquire lock on the object





SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Context switching is basically used for multithreading..
You can google for Context Context Switching.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Shanky Sohar wrote:
Yield is a static method..


Both sleep() and yield are static method. You can't tell other thread to sleep or yield.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Abimaran Kugathasan wrote:
Shanky Sohar wrote:
Yield is a static method..


Both sleep() and yield are static method. You can't tell other thread to sleep or yield.


Sorry Sir,But Red part seems to be confusing for me.Could you please tell.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Shanky Sohar wrote:
Sorry Sir,But Red part seems to be confusing for me.Could you please tell.

OK, they are static methods, so if you invoked these methods on a thread instance, what would happen then?
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Abimaran Kugathasan wrote:
Shanky Sohar wrote:
Sorry Sir,But Red part seems to be confusing for me.Could you please tell.

OK, they are static methods, so if you invoked these methods on a thread instance, what would happen then?

it will acquire a lock on the class.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Not in that context, there is no instances in the static context. So we can't logically invoked static methods on instances. That's what I mentioned! I think, now it's clear!
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Abimaran Kugathasan wrote:Not in that context, there is no instances in the static context. So we can't logically invoked static methods on instances. That's what I mentioned! I think, now it's clear!


We can invoke static method from a instance also.there will not be any problem..and also we can also invoke yield and sleep on the instance of a same object.

one will occupy a look on the class object other will occupy a look on a Class
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Shanky Sohar wrote:
We can invoke static method from a instance also.there will not be any problem..and also we can also invoke yield and sleep on the instance of a same object.

one will occupy a look on the class object other will occupy a look on a Class


I don't mean this. You can do whatever you've said! But, let's take threadA and threadB as two thread instances, and in you run() method of threadA, you invoked the sleep() method on threadB. What would happen?

static methods don't have any idea about instances of that class. So, the static methods are invoked on class basics, therefore, sleep() method will invoked on whatever the thread running at the time.

Hope you understood this! Is this para makes sense? Thanks!
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi ranchers,
Could you Please cite an example for this so that it can we used for later reference and be explained properly with comments...I have understood the basic differences but if you could oblige by citing an example it would be great enough.



Thanks and Regards
Deepak Lal
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

try to recognize what should be the output here



this is related to my post above
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sleep and Yield state
 
Similar Threads
Doubt in Thread.yield()
What puts a thread in a waiting state?
difference between Yield,wait and sleep
Thread topic Help needed
Sleep or Yield in a infinite loop