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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:08:49+00:00 2026-06-13T18:08:49+00:00

There is a scenario in my Application where a Thread under its run method

  • 0

There is a scenario in my Application where a Thread under its run method while(true) condition does this OPeration .

Under its while true condition , it continuasally fetches information from a External Data Source(Memcache) , adds / removes content from it, depending on newly logged in or logged out users , and finally updates it back to Memcache .

Some Snipet of code

public class MonitoringThread extends Thread 
{
public void run() {
while(true)
String  response = Memcache.get("USERINFO");
// Some checks will be made on response and builds a new response and updates back to Memcache .
synchronized (response)
{
Memcache.set("USERINFO" , response) ;
}
}
}

My question is

  1. I have kept a Synchronized block on a set operation (Is this correct ) or it should be done while doing a get Operation ??

  2. And the parameter that is passed to the Synchronied block , whether this should be this (the class itself) or the String response

Some more information .

Our Application runs on 5 Web Server instances controlled by a Load Balancer . ( All these 5 Instances will do that get Operation and Updates Data back )

Please share your ideas .

Thanks for reading , have a great day .

Edited Part

Thanks for the response ,

I have changed the synchronized block parameter , please let me know what is fesable appproach below

Option 1

Using synchronied block in the Thread

synchronized (Memcache.class)
{
Memcache.set("USERINFO" , response) ;
}


class Memcache
{
public static boolean set(String key, Object value) {
MemcacheClient.set(key, 0, value);
}

Or

Using Synchronied block in the Memcache class itself

class Memcache
{
synchronized (Memcache.class)
{
public static boolean set(String key, Object value) {
MemcacheClient.set(key, 0, value);
}

// other methods
}

  • 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-13T18:08:51+00:00Added an answer on June 13, 2026 at 6:08 pm

    I don’t know what you’re trying to do with this synchronized block, and why you need synchronization in the first place. What is the shared state that you want to protect with a synchronized block?

    Anyway, what’s sure is that this snippet doesn’t make much sense. synchronized(response) means that two threads won’t be able to execute the synchronized block if they both received the same String instance (the same object, not just the same characters) from the cache. This is very unlikely, and is probably not what you want.

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

Sidebar

Related Questions

This is a multi threaded scenario. The main thread handles the application and UI
The scenario is this: I have Java swing application with JFrame. There is textarea
The usual scenario, there is an MFC/Win32/WTL/wxWidgets/Qt application that does something useful. It was
There is a scenario where an application tells a device on a network to
Scenario I have a Windows Forms Application. Inside the main form there is a
Scenario : There is this online questionaire that will be filled in by various
Is there a standard way to close out an application cleanly while some WaitHandle
I'm building a multi-threaded service application in Delphi XE2. Each thread serves its own
In my app i am having following scenario There is thread running in background
Scenario: There is a website with 700-900 concurrent unique visitors at any given time.

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.