jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread start() method - a doubt 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 start() method - a doubt" Watch "Thread start() method - a doubt" New topic

Thread start() method - a doubt

Jonathan Jeban
Ranch Hand

Joined: Oct 08, 2000
Posts: 52
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,
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
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 ???
Jonathan Jeban
Ranch Hand

Joined: Oct 08, 2000
Posts: 52
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).]
I agree. Here's the link: http://aspose.com/file-tools
subject: Thread start() method - a doubt
jQuery in Action, 3rd edition