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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:19:22+00:00 2026-05-29T05:19:22+00:00

I have a concurrent server where a user chooses an option to display. Every

  • 0

I have a concurrent server where a user chooses an option to display. Every time any option is selected a counter increments displaying only the total for all options selected. Individual counters are NOT required for different options; only total is required.

After referencing the JAVA API the most sensible method appeared to be using atomic integers and everything worked perfectly as expected.

I produced the following:

ServerProtocol.java

public class ServerProtocol {

private static final int ANOTHER = 2;
private static final int OPTIONS = 3;

case OPTIONS:
    if (theInput.equals("1")) {
        theOutput = "computer program description here  -- Another? Y or N";

        DownloadCounter counter = new DownloadCounter();
        counter.incrementCount();
        System.out.println(counter);

        TrackDownloads clientDetails = new TrackDownloads();
        clientDetails.trackLocalhostAddress();
        state = ANOTHER;
case ANOTHER:

DownloadCounter.java

import java.util.concurrent.atomic.AtomicInteger;

public class DownloadCounter {

    private static AtomicInteger count = new AtomicInteger(0);

    public void incrementCount() {
        count.incrementAndGet();
    }

    @Override
    public String toString() {
        return "Total Downloads =" + count;
    }
}

The problem being that my lecturer has turned around and said I cant use atomic integers!

They have supplied me with some code that I must include in my project. That is:

 int tmp = yourCounter;
     try {
         Thread.sleep(5000);
     } catch (InterruptedException ex) {
         System.out.println("sleep interrupted");
     }
  yourCounter = tmp + 1;

The problem is I don’t seem to be able to use this counter concurrently. I have so far produced the following:

ServerProtocol.java

public class ServerProtocol {

private static final int ANOTHER = 2;
private static final int OPTIONS = 3;
private static int yourCounter;

case OPTIONS:
    if (theInput.equals("1")) {
        theOutput = "computer program description here  -- Another? Y or N";

        int tmp = yourCounter;
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
             System.out.println("sleep interrupted");
        }
        yourCounter = tmp + 1;

        System.out.println("Download Total " + yourCounter);

        TrackDownloads clientDetails = new TrackDownloads();
        clientDetails.trackLocalhostAddress();
        state = ANOTHER;
case ANOTHER:    

With the above method, when 2 or clients request the option simultaneously, the yourCounter variable only increments once. I’m expecting that only a single client is granted access to the variable at once to ensure the counter remains accurate. Any ideas?

Thanks in advance.

  • 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-29T05:19:22+00:00Added an answer on May 29, 2026 at 5:19 am

    You have to use the synchronized keyword. Each access to the counter should be in a synchronized block that locks the same object: either a static synchronized method of the class, or a synchronized block that explicitly locks TheClass.class.

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

Sidebar

Related Questions

I have a task that I perform in parallel; for instance printing user selected
I have a table where new data gets filled each time a user clicks
I wrote a multi user app in c# some time age using SQL Server
I have created a concurrent, recursive directory traversal and file processing program, which sometimes
I have a fairly specific question about concurrent programming in C. I have done
I have a multithreaded application that has many concurrent operations going on at once.
I have some problems on a site with the concurrent access to a list.
I have multiple threads each one with its own private concurrent queue and all
I have a client which makes a limited number of concurrent web requests. I
While I'm familiar with concurrent programming concepts such as mutexes and semaphores, I have

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.