Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread start() method - a doubt

 
Jonathan Jeban
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jonathan Jeban
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi deekasha,
Thanx a lot!! It was nice explanation with code..
It really cleared my doubt.


-Jeban

[This message has been edited by Jonathan Jeban (edited October 14, 2000).]
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic