Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sleep and Yield state

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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




 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Context switching is basically used for multithreading..
You can google for Context Context Switching.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to recognize what should be the output here



this is related to my post above
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic