?
Solved

Costs of Synchronized methods

Posted on 2005-03-13
8
Medium Priority
?
236 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
[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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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 had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 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…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

800 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