Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 848839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:00:20+00:00 2026-05-15T07:00:20+00:00

This is a question concerning what is the proper way to synchronize a shared

  • 0

This is a question concerning what is the proper way to synchronize a shared object in java. One caveat is that the object that I want to share must be accessed from static methods. My question is, If I synchronize on a static field, does that lock the class the field belongs to similar to the way a synchronized static method would? Or, will this only lock the field itself?

In my specific example I am asking: Will calling PayloadService.getPayload() or PayloadService.setPayload() lock PayloadService.payload? Or will it lock the entire PayloadService class?

public class PayloadService extends Service {   


private static PayloadDTO payload = new PayloadDTO();


public static  void setPayload(PayloadDTO payload){
    synchronized(PayloadService.payload){
        PayloadService.payload = payload;
    }
}

public static  PayloadDTO getPayload() {
    synchronized(PayloadService.payload){
        return PayloadService.payload ;
    }
}

...

Is this a correct/acceptable approach ?

In my example the PayloadService is a separate thread, updating the payload object at regular intervals – other threads need to call PayloadService.getPayload() at random intervals to get the latest data and I need to make sure that they don’t lock the PayloadService from carrying out its timer task

Based on the responses, I refactored to the following:

public class PayloadHolder {

private static PayloadHolder holder;    
private static PayloadDTO payload;

private PayloadHolder(){        
}

public static synchronized PayloadHolder getInstance(){
    if(holder == null){
        holder = new PayloadHolder();
    }
    return holder;
}

public static synchronized void initPayload(){      
    PayloadHolder.payload = new PayloadDTO();       
}
public static synchronized PayloadDTO getPayload() {
    return payload;
}
public static synchronized void setPayload(PayloadDTO p) {
    PayloadHolder.payload = p;
}

}

public class PayloadService extends Service {   

  private static PayloadHolder payloadHolder = PayloadHolder.getInstance();

  public static  void initPayload(){        
            PayloadHolder.initPayload();        
  }

  public static  void setPayload(PayloadDTO payload){       
        PayloadHolder.setPayload(payload);      
  }

  public static  PayloadDTO getPayload() {      
    return PayloadHolder.getPayload();      
  }

     ...

Is this approach legitimate? I am also curious if it is better to do it this way or using the AtomicReference approach mentioned by Hardcoded …?
– I am keeping an instance of PayloadHolder on PayloadService simply to keep a reference to the PayloadHolder class active in the jvm for as long as the PayloadService is running.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:00:21+00:00Added an answer on May 15, 2026 at 7:00 am

    You could, as mentioned in other posts, synchronize on the class or on an explicit monitor.

    There are 2 other ways, if we assume that your are using the sychnronize only for thread-safe getting and setting of the property: volatile and AtomicReference.

    volatile

    The volatile keyword will make access to the variable atomic, meaning that reading and assigning the variable won’t be optimized by the CPUs local registers and are done atomically.

    AtomicReference

    The AtomicReference is a special class at the java.util.concurrent.atomic package, which allows atomic access to a variable-like reference. It is very similiar to volatile, but gives you some additional atomic operations, like compareAndSet.

    Example:

    public class PayloadService extends Service {   
    
    private static final AtomicReference<PayloadDTO> payload 
              = new AtomicReference<PayloadDTO>(new PayloadDTO());
    
    public static void setPayload(PayloadDTO payload){
        PayloadService.payload.set(payload);
    }
    
    public static PayloadDTO getPayload() {
        return PayloadService.payload.get ;
    }
    

    Edit:

    Your Holder seems quite confused, since you are instantiating classes only to call static Methods. A try to get it fixed with AtomicReference:

    public class PayloadHolder {
    
      private static AtomicReference<PayloadHolder> holder = new AtomicReference<PayloadHolder();    
    
      //This should be fetched through the holder instance, so no static
      private AtomicReference<PayloadDTO> payload = new AtomicReference<PayloadDTO>();
    
      private PayloadHolder(){        
      }
    
      public static PayloadHolder getInstance(){
        PayloadHolder instance = holder.get();
    
        //Check if there's already an instance
        if(instance == null){
    
          //Try to set a new PayloadHolder - if no one set it already.
          holder.compareAndSet(null, new PayloadHolder());
          instance = holder.get();
    
        }
        return instance;
      }
    
      public void initPayload(){      
        payload.set(new PayloadDTO());
    
        //Alternative to prevent a second init:
        //payload.compareAndSet(null, new PayloadDTO());
      }
    
      public PayloadDTO getPayload() {
        return payload.get;
      }
    
      public void setPayload(PayloadDTO p) {
        payload.set(p);
      }
    
    }
    
    public class PayloadService extends Service {   
    
      private final PayloadHolder payloadHolder = PayloadHolder.getInstance();
    
      public void initPayload(){        
        payloadHolder.initPayload();        
      }
    
      public void setPayload(PayloadDTO payload){       
        payloadHolder.setPayload(payload);      
      }
    
      public PayloadDTO getPayload() {      
        return payloadHolder.getPayload();      
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, this was my first question concerning that issue : SQL request to group
I have a question concerning this code which I want to run on QNX:
This is a general question concerning technology decisions for a product development. My aim
this is a different question concerning: add a connection to database not working, asp.net
I have a question concerning active record association, referring to this part of the
This is probably a long shot but I asked a question about converting one
This question is similar in concept to this one , except I see I
Very similar to this question , I am trying to convert a project that
I have a question concerning floating constants in C. In Java, the default type
This question is a dual of Constructor with by-value parameter & noexcept . That

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.