This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi All, I just started learning Threads and I got a basic doubt.....
When a thread runs a synchronized method...a lock is obtained for the object.
If t1 is the thread I created....i call it by t.start().But in the Thread class...start() method is defined as
public synchronized native void start()
So call to t.start() will obtain a lock for the object,right?? So if I call another thread t1 for the same object..then t1 has to wait till the first thread release the lock... But it is not happening.... I know for sure I am missing something..could somebody help me to clear my doubt. Thanx in advance, Jeban.
Hi johmathan, first of all let's have two points. 1) Every thread is an Object itself. 2) when an object's sync. method is getting executed by a thread then no other thread can execute that object's other sync. method until unless the first method is completed. Now let's see your doubt.
when u r creating t then a new instance Thread object is created.and when you say t.start() lock is obtained on the instance referred by t. u r saying that u create another thread t1. so this time a new instance of thread object is there and t1.start() obtains the lock on the object referred by t1. so u see since they are obtaining lock on different objects there is no problem of sync.
The problem u r stating will appear if u try running Threads1 class that I've written above .and u'll get illegalmonitorstateexception since here I'm trying to call the start of same object twice. was it clear ??? regards deekasha
Joined: Oct 08, 2000
Hi deekasha, Thanx a lot!! It was nice explanation with code.. It really cleared my doubt.
[This message has been edited by Jonathan Jeban (edited October 14, 2000).]