• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread

 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have collected few point regarding Thread.Please correct me if i am wrong anywhere.

1.Methods that will stop execution-
wait(),sleep(),join()
2.yield() causes the thread to move to Runnable state.
3.wait(),sleep(),join() causes the thread to move to Not-Runnable state.
4.wait() releases the lock.
5.sleep(),join(),yield() does not releases the lock.
6.notify() does not releases the lock immediately.
7.Threads communicates through wait(),notify(),notifyAll().
8.Calling start() twice causes Runtime Exception.

If anybody know any more points please let me know.
I have one doubt whether calling yield() will stop execution of the thread that is calling it.
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ramya jp:

1.Methods that will stop execution-
wait(),sleep(),join()
2.yield() causes the thread to move to Runnable state.
3.wait(),sleep(),join() causes the thread to move to Not-Runnable state.
4.wait() releases the lock.
5.sleep(),join(),yield() does not releases the lock.
6.notify() does not releases the lock immediately.
7.Threads communicates through wait(),notify(),notifyAll().
8.Calling start() twice causes Runtime Exception.

1. wait, sleep, join will make the thread go to the runnable state
2. if there are no other threads in the queue or only threads with a lower priority in the queue the thread MIGHT NOT leave the running state
3. incorrect, see 1.
4. correct
5. correct
6. notify and notifyAll make the thread release the lock(s) only if no more instructions follow (the synchronized method returns)
7. correct
8.correct

I hope this is correct...

Gio :-)
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in K&B pg 507,it states that in waiting/blocked/sleeping "it is not runnable but it might return to runnabe state later" so according to this
a call to wait(),sleep(),join() goes to not-runnable state....

correct me if i am wrong
 
Eusebio Floriano
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ramya jp:
But in K&B pg 507,it states that in waiting/blocked/sleeping "it is not runnable but it might return to runnabe state later" so according to this
a call to wait(),sleep(),join() goes to not-runnable state....

correct me if i am wrong


Correct Ramya,
Waiting/blocked/sleeping FOR SURE put the Thread in a non runnable state.

Regards,
 
ramaseshan T
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding one more item to the list.

If you call the run method directly with out calling 'start' method, thread is not created and it calls the 'run' function defined. No exceptions are thrown.
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ramya jp:
But in K&B pg 507,it states that in waiting/blocked/sleeping "it is not runnable but it might return to runnabe state later" so according to this
a call to wait(),sleep(),join() goes to not-runnable state....

correct me if i am wrong


I am actually an idiot!!!
I am sorry...you and the book are obviously right!!!

I apologize... :-(

Gio :-)
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you all for helping me
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any body explain me this point :

6.notify() does not releases the lock immediately.

thanks .
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean , if notify doesn't realease the lock then how the thread waintin will get the lock . what is the concept of imediately ...
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
I mean , if notify doesn't realease the lock then how the thread waintin will get the lock . what is the concept of imediately ...


It is not the notify() command that releases the lock. The one that releases the lock is the thread that currently owns it. The one that issued the notify() only tells the JVM that it is ready to release it.
[ March 03, 2005: Message edited by: Alton Hernandez ]
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meants was the same , The thread calls this line ( obj.notify(); ) will realease the lock of obj . And any other thead which is in waiting queue of obj will wake up & continue its execution ...

but why not immediately ...

Thanks .
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wait(),sleep(),join() causes the thread to move to Not-Runnable state


I do agree to the above statement on wait() and sleep() but how come join causes the thread to move to Not runnable state. It infact lets the thread to proceed until it dies.
As said in the api:

Waits for this thread to die.


Let me know if i am wrong

Thanks
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how come join causes the thread to move to Not runnable state. It infact lets the thread to proceed until it dies.

I think that depends on which thread we're talking about.

For example,


When t.join is called, the main thread becomes non-runnable. Whereas t thread proceeds until it dies.

Joyce
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
I meants was the same , The thread calls this line ( obj.notify(); ) will realease the lock of obj . And any other thead which is in waiting queue of obj will wake up & continue its execution ...

but why not immediately ...

Thanks .


rathi,

Do not confuse between locks and threads. They are two different things. Locks are inherent on every objects, not just Thread objects. And remember that a thread is a process which is created by the Thread object.

A notify() is a request by a thread to re-acquire the lock of an object. It may or may not get that request immediately if other threads have a lock on that object.
[ March 03, 2005: Message edited by: Alton Hernandez ]
 
Eusebio Floriano
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alton Hernandez:

A notify() is a request by a thread to re-acquire the lock of an object. It may or may not get that request immediately if other threads have a lock on that object.


Alton,
The Thread that calls notify() is the one who has the lock, doesn�t it ?
Actually, It just can calls notify() in a shyncronized block.
Do you agree ?
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vinicius Boson:


Alton,
The Thread that calls notify() is the one who has the lock, doesn�t it ?
Actually, It just can calls notify() in a shyncronized block.
Do you agree ?


Oops..(typing too fast without thinking again). Thank you for pointing that out. What I meant to say was:

A notify() is a request by a thread for others to re-acquire the lock of an object. Others may or may not get that request immediately if the thread still have a lock on that object.

May I also add that a lock is released only when the thread completes the monitor region (the synchronized block) or by issuing a wait() command. So unless these two things are performed, the lock remains on thread regardless how early the notify() command is issued.

[ March 03, 2005: Message edited by: Alton Hernandez ]
[ March 03, 2005: Message edited by: Alton Hernandez ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic