Making a class thread-safe

Posted on 2006-04-16
Last Modified: 2012-08-14
Hi All,

Can anyone tell me how they would make a simple class like the one below thread-safe?

  public class threadSafe {
        private ThreadSafe makeThreadSafe;
        public void setMakeThreadSafe(ThreadSafe threadVar) {
            makeThreadSafe = threadVar;
        public Bar getMakeThreadSafe() {
            return makeThreadSafe;

I'm trying to learn a bit more a thread safety, obviously The class could be made a singleton and I could synchronize the set method.  Is there anything else I could do to make a simple class thread-safe.  Thanks.

Question by:zozig
    LVL 14

    Expert Comment

    the one you provided does not need thread safty

    from the book java performance and tuning

    Variables shared between multiple threads (e.g., instance variables of objects) have atomic assignment guaranteed by the Java language specification for all data types except longs and doubles. Actually, the storing of a value into a variable takes two primitive operations, a store and a write. However, the language specification also states that once a store operation occurs on a particular variable, no other store operation is allowed on that variable until the write operation has occurred. The (original[5]) specification allows longs and doubles to be stored in two separate sets of store+write operations, hence their exception to atomicity. A similar atomic specification applies for reading variables

    This means that access and update of individual variables does not need to be synchronized simply to avoid corruption (as long as they are not longs or doubles). If a method consists solely of a single variable access or assignment, there is no need to make it synchronized for thread-safety, and every reason not to do so for performance.

    LVL 14

    Expert Comment

    > is also an atomic assignment
    this assignment I think is also atomic
    LVL 86

    Expert Comment

    Any methods should be marked synchronize that you want to be thread-safe
    LVL 23

    Accepted Solution


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    scoresSpecial  challenge 13 34
    array220 challenge 8 31
    Passing list of object to Oracle Database Procedure 3 32
    mapShare challenge 13 41
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    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 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…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now