Solved

Locking mechaniam in java

Posted on 2001-06-05
10
229 Views
Last Modified: 2008-03-04
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
0
Comment
Question by:sobhan_genie
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
 
LVL 19

Expert Comment

by:Jim Cakalic
Comment Utility
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
 
LVL 7

Expert Comment

by:Igor Bazarny
Comment Utility
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
 
LVL 92

Accepted Solution

by:
objects earned 25 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Assisted Solution

by:Igor Bazarny
Igor Bazarny earned 25 total points
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> it just wasn't clear from your comment.

Correct, and you clarified it. Thank you :)
0
 
LVL 7

Expert Comment

by:Igor Bazarny
Comment Utility
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility


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
 
LVL 19

Expert Comment

by:Jim Cakalic
Comment Utility
OK by me.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now