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 8766127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:28:18+00:00 2026-06-13T16:28:18+00:00

I have this piece of code below as shown . Our Application runs on

  • 0

I have this piece of code below as shown .
Our Application runs on 5 web servers controlled by a Load Balancer ,all connecting to one Memcache instance .

I guess that this piece of synchrnozation works only for one Instance .

Please let me know how can i synchrnoze this piece of code when 5 web servers are trying to access the Memcache

public class Memcache {
    private MemcachedClient memclient = null;  
    private static Memcache instance = null;

    public static Memcache getInstance() {
        if (instance == null) {
            try {
                synchronized (Memcache.class) {
                    instance = new Memcache();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return instance;
    }

    private Memcache() throws IOException {
        MemcachedClientBuilder builder = new XMemcachedClientBuilder();
        memclient = builder.build();

    }

}
  • 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-06-13T16:28:20+00:00Added an answer on June 13, 2026 at 4:28 pm

    Why not initialize it like this?

    private static Memcache instance = new Memcache();
    

    Bare in mind that what you tried to achieve at the synchronization here is problematic,
    As two threads might pass the (if (instance == null) (a context switch might be after that line)
    So you can consider the double check pattern,
    BUt at some version of java there is a problem with it.
    At the link I provided , there is info about problem, and
    in this link, you can read about Singleton with the volatile keyword.

    I still would go for the option I suggested above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a piece of code shown below #include <stdio.h> #include <stdlib.h> void Advance_String(char
I have a piece of ada code shown below which is a simple switch
I have this piece of code in a function. I want to print the
I have this piece of code: public static DateDialogFragment newInstance(Context context, DateDialogFragmentListener listener) {
I have this piece of code: @Entity @Table(name = MOVERS) public class MOVers implements
I have this piece of code for my images, so the opacity can lower
I have this piece of code, <div id=event style=display:none;> <!-- initially hidden --> <div
I have this piece of code which check the last customer_id from the db.
I have this piece of code List<Class<? extends SubApplication>> appClasses = new ArrayList<Class<? extends
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();

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.