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.
You have to use the
synchronizedkeyword. Each access to the counter should be in asynchronizedblock that locks the same object: either astatic synchronizedmethod of the class, or a synchronized block that explicitly locksTheClass.class.