Locking mechaniam in java

Hello All,

          I would appreciate any explanation or pointers to the following.

1) How syncronized works in java. I heard it locks the whole object I mean syncronized methods of the object. Is it true?

2) If I have syncronized block inside a function how does this work exactly....

bye,
Sobhan
sobhan_genieAsked:
Who is Participating?
 
objectsCommented:
Fellow MVP,

Thanks for the comments.

> There is only one monitor per object, and once
> synchronized block or method started, other synchronized
> _on_this_object_ methods or blocks will be blocked

Synchronized blocks can specify whatever object they want to acquire the lock on, it doesn't have to be on_this_object.

Also I wasn't inferring independence between the two, I was simply addressing how the question was asked.

> what is 'non-static block' :-)?

Oops, cut and paste bug :)

0
 
objectsCommented:
1) while a synchronized method is executing, attempting to call any other non-static synchronized method on that object will block until the method executing exits.

2) while a synchronized block is executing, attempting to call any other non-static synchronized block on that object will block until the block executing exits.

0
 
objectsCommented:
The main purpose is to stop two threads concurrently changing an objects state/data.
If a method does not make any changes to the objects state then there is no need to synchronize the method.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Jim CakalicSenior Developer/ArchitectCommented:
Object's comments are correct. For additional information on thread-safety and how and when to use synchronization in your design, I would recommend reading this Design Techniques article by Bill Venners (author of "Inside the Java 2 Virtual Machine"):
    http://www.artima.com/designtechniques/threadsafety.html

Best regards,
Jim Cakalic
0
 
Igor BazarnyCommented:
Hi,

Don't interpret first Object's comment as method and block synchronization independence. There is only one monitor per object, and once synchronized block or method started, other synchronized _on_this_object_ methods or blocks will be blocked. So, this constructs are almost equivalent:

public synchronized void method(){
    // method body
}

public void method(){
    synchronized(this){
        // method body
    }
}

Static methods synchronize on correspondent Class instance, so when synchronized static method is executing, synchronized instance method will not block.  This methods can be invoked from different threads:

public static synchronized void staticMethod(){
}

public synchronized void instanceMethod(){
}

Nested calls of synchronized methods or blocks will not be blocked. In following code nestedMethod() call will not block:

public synchronized void nestedMethod(){
}

public synchronized void nestedCaller(){
     nestedMethod();
}

BTW Object, what is 'non-static block' :-)?

Regards
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com 
0
 
Igor BazarnyCommented:
Hi,

> Synchronized blocks can specify whatever object they want to acquire the lock on, it doesn't have to be
> on_this_object.
Yes, but they will prevent execution of synchronized methods of object on which they synchronize.

>  Also I wasn't inferring independence between the two, I was simply addressing how the
> question was asked.
I don't believe you do, it just wasn't clear from your comment.

Regards,
Igor Bazarny
0
 
objectsCommented:
> it just wasn't clear from your comment.

Correct, and you clarified it. Thank you :)
0
 
Igor BazarnyCommented:
Hi,

OK, that was fun, but...
Sobhan, do you have more questions on the topic? Are you ready to award points?

Regards,
Igor Bazarny
0
 
sudhakar_koundinyaCommented:


No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


--  split Points between objects and bazarny


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
0
 
Jim CakalicSenior Developer/ArchitectCommented:
OK by me.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.