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

  • Home
  • SEARCH
  • 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 8121953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:40:27+00:00 2026-06-06T05:40:27+00:00

I intend to create a realtime counter. So one user can incremenet the counter

  • 0

I intend to create a realtime counter. So one user can incremenet the counter value for specific key. Whilst another gets the updated count value via an ajax request (either in a loop, or using some long polling method). I will use a spring controller, which will inject the service class can I do something like below, or is there a better way :

@Service
public MyService{

//instance variable in spring injected service class, not sure if this correct
static final Map<String, Integer> myMap;


public void add(String key){
  Integer count = myMap.get(key);
  count++;
  myMap.put(key, count);
}

//accessed via ajax loop (and controller), if value changes update display
public Integer getCount(String key){
  return myMap.get(key)
}

@PostConstruct
public load(){
  myMap = new HashMap<String, Integer>(10){{//initialize}};
}

Edit there are a few answers but it is not clear which is the best : Synchronize the add method ? Create map in another class(annotated repository) and inject that ? Something else ?

  • 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-06T05:40:31+00:00Added an answer on June 6, 2026 at 5:40 am

    You can, but need to be aware of those problems:

    • the map is empty initially, but you never check for null counters;
    • the add() method doesn’t modify the counter in the map. You need to put the counter back in the map after incrementing it, since Integer is immutable. Or you need to store mutable counters inside the map
    • several threads are accessing the map without any kind of synchronization, which will lead to bugs, erratic behavior, or exceptions
    • this strategy will obviously fail in case your app is clustered among several servers
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I create a new file, in which I intend to write in
I want to create an application that can transform text the user enters in
I intend to create a checker function which will be used by another functions.
Why can't C++ determine that I intend to create a unique_ptr<A> with this syntax?
I intend to create asp.net pages using Visual Studio 2008. Preferably, the pages should
I have a data that looks like this . And I intend to create
I intend to create an HTML report from a perl script on Linux/Unix side.
I intend to make a program that does the following: Create an NSArray populated
I need to create a webcam chat which i intend to use in my
I know you can create a basic view to do this, but what I

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.