Making a class thread-safe

Posted on 2006-04-16
Medium Priority
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
  • 2
LVL 14

Expert Comment

ID: 16465319
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

ID: 16465332
> is also an atomic assignment
this assignment I think is also atomic
LVL 86

Expert Comment

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

Accepted Solution

Siva Prasanna Kumar earned 2000 total points
ID: 16466459

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 functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month16 days, 16 hours left to enroll

862 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