File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Question?? 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 Question??" Watch "Thread Question??" New topic
Author

Thread Question??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9383
    
    2

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 ]

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
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


SCJP1.5(86%)<br />SCWCD1.4(95%)<br />SCBCD1.3(92%)<br />IBM 252
Sai Surya
Ranch Hand

Joined: Feb 08, 2006
Posts: 459

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.


Sai Surya, SCJP 5.0, SCWCD 5.0, IBM 833 834
http://sai-surya-talk.blogspot.com, I believe in Murphy's law.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9383
    
    2

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

Joined: Mar 22, 2006
Posts: 45
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

Joined: Nov 04, 2006
Posts: 22
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

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Question??