Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
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

Joined: Aug 17, 2004
Posts: 150
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 :-)


SCJP 1.4
meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
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

Joined: Mar 07, 2004
Posts: 237
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,


SCJP 1.4 / 5.0 - SCBCD 1.3 - SCWCD 1.4 - IBM 484
ramaseshan T
Ranch Hand

Joined: Feb 17, 2005
Posts: 30
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.


Ramaseshan T<br />SCJP 1.4
Giovanni De Stefano
Ranch Hand

Joined: Aug 17, 2004
Posts: 150
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

Joined: Jan 24, 2005
Posts: 219
Thanks you all for helping me
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
can any body explain me this point :

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

thanks .
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: May 30, 2003
Posts: 443
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Jul 19, 2004
Posts: 298

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

Joined: Jul 11, 2003
Posts: 1392
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

Joined: May 30, 2003
Posts: 443
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

Joined: Mar 07, 2004
Posts: 237
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

Joined: May 30, 2003
Posts: 443
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread