Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with kathy sierra book

 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the sleep or wait is over, or an object's lock becomes available, the
thread can only reenter the runnable state. It will go directly from waiting to
running (well, for all practical purposes anyway).

It should be that it will not directly from waiting to running
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runnable state is the very first state in Threads life cycle and thats too comes once, when object get instantiated. Once the run() method gets called, it becomes running.
 
Juri Smolarek
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Rohankar wrote:Runnable state is the very first state in Threads life cycle and thats too comes once, when object get instantiated. Once the run() method gets called, it becomes running.


First state is "new", "runnable" is 2nd.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob you are write the thread goes to runnable state not running state. It should be misprint.


A thread will have 4 stages in its lifecycle:

new ---> runnable------>running-------->dead

the fifth stage which may come is waiting/blocking state after which it goes back to runnable state.

new comes only once and after the thread is dead no other states can ever come.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Juri Smolarek wrote:
Sagar Rohankar wrote:Runnable state is the very first state in Threads life cycle and thats too comes once, when object get instantiated. Once the run() method gets called, it becomes running.

First state is "new", "runnable" is 2nd.

When you create "new" Thread, its become "runnable".
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Rohankar wrote:When you create "new" Thread, its become "runnable".

AFAIK, this is not true. When you instantiate a thread, its not runnable. The way I know it, is that a thread is runnable when it has been started (using start method) but it is actually not running i.e. the CPU isn't executing it at this point in time. The thread may switch between runnable and running state multiple times during its lifetime which we don't have to worry about most of the times. The yield method is used to make a running thread runnable...
 
Juri Smolarek
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nope! (nope to Sagar Rohankar 14:27:41)
page 708

invoking start() changes state to runnable
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused in "running" and "runnable" state. I gone with the simple English which forced me to conceive "runnable" means the "the thread has ability to run(execute), but isn't executing at the time" and "running" means running.
But in Java, "runnable" means "running".. So I understood my mistake, thank you for pointing me out.
 
Juri Smolarek
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
runnable means: thread has the ability to be executed by the scheduler. but there is no guarantee, that scheduler will comes immediately. if he does not, that's the gap between runnable and running. if he does, state will be changed to running.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Rohankar wrote:I'm confused in "running" and "runnable" state. I gone with the simple English which forced me to conceive "runnable" means the "the thread has ability to run(execute), but isn't executing at the time" and "running" means running.
But in Java, "runnable" means "running".. So I understood my mistake, thank you for pointing me out.


your understanding is right runaable means a thread has ability to run that is to come to running state, but when will it come to running state depends upon scheduler.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh.. How come I missed that "scheduler" part,.. Crystal clear now. Thanks to everyone. Gotta brush up some Thread things.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic