Solved

Locking mechaniam in java

Posted on 2001-06-05
10
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6158481
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
ID: 6158486
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
ID: 6159223
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6160938
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
ID: 6161802
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
 
LVL 7

Assisted Solution

by:Igor Bazarny
Igor Bazarny earned 25 total points
ID: 6161927
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
ID: 6161942
> it just wasn't clear from your comment.

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

Expert Comment

by:Igor Bazarny
ID: 6174039
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
ID: 8898990


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
ID: 8904148
OK by me.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses

751 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