【发布时间】:2017-10-19 15:46:19
【问题描述】:
我想用 Java 实现我自己的信号量(我知道,只是为了练习,有 Semaphore 类) 我是这样实现的:
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(但更接近,在 10 个案例中有 9 个大于 9990)。
你知道为什么会这样吗?我的信号量是错误的还是在启动线程时做错了什么?
【问题讨论】:
-
您似乎把
take和release搞混了。take(第一次操作)必须等待,release必须通知。 -
你说得对,我交换了方法名称,这很有效,我只是有好的方法,但名称错误(接下来是我使用不当),谢谢。
-
您尝试使用
AtomicInteger吗?
标签: java multithreading semaphore