信号我想用Java实现我自己的旗语(只是为了练习,我知道,有Semaphore类) 我已经实现了它这样的:我自己在java中
public class MySemaphore {
private int value = 1;
public synchronized void take() {
this.value++;
this.notify();
}
public synchronized void release(){
while (this.value == 0) {
try {
wait();
} catch (InterruptedException e) {
}
}
this.value--;
}
}
我想使用它在这样的线程:
public class MyThread extends Thread {
private static MySemaphore semaphore = new MySemaphore();
public void run(){
for (int i = 0; i < 100; i++) {
semaphore.take();
try {
Main.myVariable += 1;
semaphore.release();
} catch (Exception e){
System.out.println("Exception" + e.getMessage());
}
}
}
}
我开始和加入线程这样的:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static int myVariable = 0;
private static int threadsNumber = 100;
public static void main(String[] args) {
List<Thread> allThreads = new ArrayList<>();
for (int i = 0; i < threadsNumber; i++) {
allThreads.add(new Thread(new MyThread()));
}
for (int i = 0; i < threadsNumber; i++) {
allThreads.get(i).start();
}
for (int i = 0; i < threadsNumber; i++) {
try{
allThreads.get(i).join();
} catch (Exception e){
System.out.println(e.getMessage());
System.out.println("********************************");
}
}
System.out.println("Result is " + myVariable);
}
}
我只是想增加一个变量10000次并收到结果。没有信号量,结果小于10000(如9923,9684),这是由非增量原子性引起的。我想用信号量来保护这个变量。
不幸的是,结果仍然小于或等于10000(但更接近于9990以上的10个案例中的9个)。 你知道为什么会发生吗?我的信号量是错误的还是在启动线程时出错?
看来你得到了'take'和'release'混合起来。 'take'(第一次操作)必须等待,'release'必须通知。 – JimmyB
你说得对,我交换了方法名称,这很有效,我只是有很好的方法,但名称错了(接下来,我用它们不正确),谢谢。 – Dawid
你尝试过使用AtomicInteger吗? – diginoise