Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread topic Help needed

 
ramya ray
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
Please guide what can be circumstances a thread can move from running to runnable..

I think one is calling yield ,other is Thread schedular means higher priority thread has come or based on time slicing...

Are there any others .Please help.


Thanks ,
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I am not sure as to what this has to do with the SCJP -- but then again, I don't know what's on the test these days.

Anything that can cause the current thread to block, should cause a context switch -- reading/writing to a file, reading/writing to a network, synchronizing on a lock that is already owned, etc.

Henry
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

Wouldn't be "some time passed" also be a cause? I think the JVM switches from thread to thread also in a time dependent way. I mean independently of prioties or calling yield().

Another question: Does this also count?
"The scheduler of the system selects another application than JVM."

Yours,
Bu.
 
ramya ray
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,
whaterver be in exam these days, but it is covered by K & B book can i be that much sure

Thanks .
[ February 15, 2007: Message edited by: ramya ray ]
 
Cheryl Gray
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it would be platform dependent. Some will offer time slicing and others will offer thread priorities. I guess calling sleep() would be a way too.
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sleep() wil not put a thread from running to runnable.

A Thread wil go back to the runnable state after the time which is specified in te sleep command.
 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait/sleep/yield are non-runnable states.

The following is the life cycle of a thread after invoking a start method using Thread object.

1) runnable
2) running
3) non-runnable(sleep/wait/yield)
4) dead

yield ---> when a thread calls this method it steps back to to to a non runnable the other threads having same priority or higher than the current thread are given chance to go into running state. This is arbitrary it is quite possible that a thread might go to a non runnable state and the same thread comes to runnable state and then directly go to running state without giving chance to other threads (of same priority). Please remember, yield is a static method.

sleep ---> Sleep will put a current running thread to sleep for a millisecond defined in its argument list (eg: sleep(1000) implies 1000 millisecond). This method will put the current thread into non runnable state. the thread can be back to runnable state after the time elapsed to it gets over. sleep is static method belong to Thread class.

OS is mostly responsible for scheduling threads to run, till what time etc... If i guess right Windows implements time-slicing and linux schedules according to thread priorities.
[ February 15, 2007: Message edited by: Atul Savant ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic