File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Question??

 
Joe Harry
Ranch Hand
Posts: 10047
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Consider the code below,


Ranchers, please help me with this question. It would be great if anyone explains me the flow. How can we say that the lock of the object is with the mail thread?? What does synchronized(myrunnable) mean...and where the thread t1 waits?? Does it wait once it enters the synchronized block??on what are we locking??? I fail to understand the flow.

Thanks in advance.
[ November 08, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
prashanth kumar
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Main thing to understand here is that
1)sleep() method never releases all the held locks unlike wait() method.
So main thread wont release any locks.
2)You are synchronising on same object instance and only one thread can hold lock at a time on same object instance.

So the output would be A,B and C....hopefully iam right

Cheers
Prashanth
 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code will NOT compile, because, sleep() method throws InterruptedException which is checked exception. So you have got to handle it with try/catch or declare it in throws clause.

- Surya.
 
Joe Harry
Ranch Hand
Posts: 10047
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sai Surya,

I have declared the exception in my main method. The code compiles and gives me ABC as O/P. I just want to know the flow here. Anyone please explain me. Help me please???
 
Ganesh Pujar
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some changes in the code, but the o/p is still the same!
Lets look @ this first
[B][/B]

Expln:
In both main and run method we are syn on the myrunnable object, so @ any give time one has run complete before the other runs
Keeping that in mind,

Before calling start, main thread syn on myrunnable, so this block has to be completed first.
And then the run method runs.

Ranchers Please correct me if i'm wrong.
 
Prashant kumar Singh
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
What i think want to express here

I am not so confidance here, but..
In above code there are two synchronised blocks.So by the concept of Object locking.If a thread enters in any synchronised block or Synchronised methods then it has to keep first thats object's lock key.And in java there is only one key is allowed, not more than one .So possibility for another thread to enter in that block only when if first thread completes its task and release the lock(avail the key).So
First of all s[0],s[1],s[2] are pointing to 1,2,3 String type object.But in Sync block these instances are pointing to A,B,C strings so here block type local variables are having higher accessbilty than first.So it is printing ABC

If i am wrong please explain it

Thanks
Prashant Kumar Singh
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

yes, as Ganesh said.
Plus: It is guaranteed that the main thread runs first. So he is the first thread that gets the key of the Runnable object (there is only one).
So the main thread has to leave its synchronized block first, before Thread t1 can run().

Sleep would not release the key, only wait does that.

Comparing sleep and wait:


prints
sleep
ABC
wait
123

There is no notify in BurkisTest, it's using the wait timeout.

Yours,
Bu.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic