?
Solved

Costs of Synchronized methods

Posted on 2005-03-13
8
Medium Priority
?
249 Views
Last Modified: 2010-03-31

Are there any costs associated with synchronized methods?  I have multiple threads where each thread uses a single instance of a utilities class.  It's unlikely but possible that different threads could make concurrent calls to the same method in the utilities class.  I'm inclined to declare all the utilities class methods synchronized to be safe, but want to understand if there are any costs to synchronized methods.

John K.
0
Comment
Question by:jkavx
  • 4
  • 3
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13531323
0
 
LVL 92

Expert Comment

by:objects
ID: 13531327
you only need to sychnonise blocks of code that are not thread safe.
0
 

Author Comment

by:jkavx
ID: 13531576
If the method will always return the same result regardless of which thread calls it, is it thread safe?  The determining conditions will always be the same regardless of which thread makes the call.  So if the result of a method depends on the status of a project and the user, and the method iterates some cached data applying business rules for a given status and user, and that given status and user is the same for each thread, will there be problems if the method is not synchronized?
0
Independent Software Vendors: 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!

 

Author Comment

by:jkavx
ID: 13532609
In the example below, if the static method is called nearly simultaneously,  does the second caller's

public static boolean isEditable(String sStatus) {

    boolean bRtn = false;

}
0
 

Author Comment

by:jkavx
ID: 13532616

Sorry, didn't mean to send that.  Is this static method of a utilities class thread safe?

public static boolean isEditable(String sStatus) {
 
   if (sStatus.equals("A")
     return true;
   else
     return false;
}
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 13532669
yes (as it only deals with local variables)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13532744
>> in the utilities class

It goes against the concept of utilities classes to have synchronization in the methods, I must say. They should always be thread-safe ;) and in your case, seem to be. Synchronization is desirable only when you are sharing some data-members across multiple threads, etc.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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
Course of the Month8 days, 19 hours left to enroll

621 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