File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronization lock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronization lock" Watch "Synchronization lock" New topic

Synchronization lock

jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134
class MyRunner implements Runnable{
public void run(){
synchronized void doThis(){

class Test{
public static void main(String[] args){
MyRunner r1 = new MyRunner();
MyRunner r2 = new MyRunner();

Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);


A few questions related to synchronisation based on the above programme.
1. when you say t1.start(), which object is locked - t1, r1 or 'this'?
2. t2 can't execute doThis() method before t1 completes with it - yes or no? why?
3. does it make a difference to your above answer, if t1 and t2, takes the same argument, r1 in the constructors.

This might help me undertand the synchronizaion better.

Thanks in advance.
Naveen Zed
Ranch Hand

Joined: Aug 11, 2006
Posts: 61
for question number1==> r1 will be locked
for question number2==> No, because the object r1 only locked not r2
for question number1==> Yes, that will make a difference because lock will be on same object

And the last thing, its better to try yourself for these type of things. In your code you are not processing any thing in method doThis().If you put something in that you will know automatically what is happening inside.That is the good thing that all the successful exam writers and authurs prefer.

jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134
Thanks Naveen.

Re: your advice, e'one knows that if you try it yourself, you will learn it better. And I think people seem to try themselves before posting it to this forum. But sometimes, you can just go crazy when you are reading too much of logic and trying hard to imbibe complicated info/concepts. At times it may be a simple concept but however hard you try by thinking or coding, you fail to get a grip of it. This is exactly how it'd be ->
In such a situation this forums comes handy, I think; it worked well for me and Im sure it did for many.

You know what I mean..take it easy..I think I need a break now
I agree. Here's the link:
subject: Synchronization lock
It's not a secret anymore!