Solved

Locking mechaniam in java

Posted on 2001-06-05
10
255 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 42
oracle 11g 23 106
hibernate example for saving data 19 52
How to execute a Python program and gather return output in Java 2 40
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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

840 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