Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Locking mechaniam in java

Posted on 2001-06-05
10
Medium Priority
?
284 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 75 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 75 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

927 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