【发布时间】:2011-12-14 19:13:35
【问题描述】:
我正在尝试存储任何客户端从 ServerProtocol 类请求描述的次数。
目前,每次有新客户端加入时,计数器都会从零开始递增。有任何想法吗?
计数器类:
public class Counter {
private int counter;
public synchronized int get() {
return counter;
}
public synchronized void set(int n) {
counter = n;
}
public synchronized void increment() {
set(get() + 1);
}
}
来自 ServerProtocol 类的片段:
case OPTIONS:
if (theInput.equals("1")) {
theOutput = "computer program description here -- Another? Y or N";
counter.increment();
System.out.println(counter.get());
state = ANOTHER;
上面的 println 方法是在一个服务器类中将计数器的当前值打印到终端中:
ServerProtocol 类:
public class ServerProtocol {
private static final int TERMS = 0;
private static final int ACCEPTTERMS = 1;
private static final int ANOTHER = 2;
private static final int OPTIONS = 3;
private int state = TERMS;
public String processInput(String theInput) {
String theOutput = null;
Counter counter = new Counter();
switch (state) {
case TERMS:
theOutput = "Terms of reference. Do you accept? Y or N";
state = ACCEPTTERMS;
break;
case ACCEPTTERMS:
if (theInput.equalsIgnoreCase("y")) {
theOutput = "1. computer program 2. picture 3. e-book";
state = OPTIONS;
} else if (theInput.equalsIgnoreCase("n")) {
theOutput = "Bye.";
} else {
theOutput = "Invalid Entry -- Terms of reference. Do you accept? Y or N";
state = ACCEPTTERMS;
}
break;
case ANOTHER:
if (theInput.equalsIgnoreCase("y")) {
theOutput = "1. computer program 2. picture 3. e-book";
state = OPTIONS;
} else if (theInput.equalsIgnoreCase("n")) {
theOutput = "Bye.";
} else {
theOutput = "Invalid Entry -- Another? Y or N";
state = ACCEPTTERMS;
}
break;
case OPTIONS:
if (theInput.equals("1")) {
theOutput = "computer program description here -- Another? Y or N";
counter.increment();
counter.get();
state = ANOTHER;
} else if (theInput.equals("2")) {
theOutput = "picture description here -- Another? Y or N";
state = ANOTHER;
} else if (theInput.equals("3")) {
theOutput = "e-book description here -- Another? Y or N";
state = ANOTHER;
} else {
theOutput = "Invalid Entry -- 1. computer program 2. picture 3. e-book";
state = OPTIONS;
}
break;
default:
System.out.println("Oops");
}
return theOutput;
}
}
【问题讨论】:
-
每次客户端加入时你都调用processInput吗?每次调用该方法时,您都会创建一个新计数器。
-
除其他答案外:如果您先递增然后获取,则可能有多个线程同时递增,然后在当前获取。所有线程只会看到相同的计数器值。考虑使用 AtomicInteger 及其 incrementAndGet 方法。
-
@Megacan 是的,每次都会调用 processInput。如下所述,使变量静态似乎可以完成这项工作
标签: java concurrency atomic