Java 在两个线程之间交替(生产者-消费者)
我试图编辑我的程序,以便交替调用Java 在两个线程之间交替(生产者-消费者),java,Java,我试图编辑我的程序,以便交替调用递增器和递减器类,首先执行递增器。我的目标是能够在每次递增/递减后打印共享变量的值(sharedValue),并希望看到它在1和0之间切换。下面是我的main类、一个semaphore类和incrementer类的代码(有一个类递减器,它的样式与icrementer相同,所以我没有包含它) 主类 public class Main extends Thread { private static int sharedValue = 0; priva
递增器
和递减器
类,首先执行递增器
。我的目标是能够在每次递增
/递减
后打印共享变量的值(sharedValue
),并希望看到它在1
和0
之间切换。下面是我的main
类、一个semaphore
类和incrementer
类的代码(有一个类递减器
,它的样式与icrementer
相同,所以我没有包含它)
主类
public class Main extends Thread {
private static int sharedValue = 0;
private static Semaphore semaphore = new Semaphore(1);
static int numberOfCycles = 20000;
public static void increment() {
semaphore.down();
sharedValue++;
semaphore.up();
}
public static void decrement() {
semaphore.down();
sharedValue--;
semaphore.up();
}
public static void main(String[] args) throws InterruptedException {
incrementer inc = new incrementer(numberOfCycles);
inc.start();
inc.join();
decrementer dec = new decrementer(numberOfCycles);
dec.start();
dec.join();
System.out.println(sharedValue);
}
}
信号量类
private int count;
// Constructor
public Semaphore(int n) {
count = n;
}
// Only the standard up and down operators are allowed.
public synchronized void down() {
while (count == 0) {
try {
wait(); // Blocking call.
} catch (InterruptedException exception) {
}
}
count--;
}
public synchronized void up() {
count++;
notify();
}
递增类
public class incrementer extends Thread{
private int numberOfIncrements;
public incrementer(int numOfIncrements){
numberOfIncrements = numOfIncrements;
}
public void run(){
for(int i = 0; i <= numberOfIncrements; i++){
Main.increment();
}
}
}
公共类递增器扩展线程{
私有整数增量;
公共递增器(整数递增){
numberOfIncrements=numOfIncrements;
}
公开募捐{
for(int i=0;i使主线程等待递增程序完成,然后启动递减程序,然后等待递减程序完成。如果希望它们同时运行,请删除两个线程。Join调用:
public static void main(String[] args) throws InterruptedException {
incrementer inc = new incrementer(numberOfCycles);
decrementer dec = new decrementer(numberOfCycles);
inc.start();
dec.start();
}
要在每次递增或递减后打印共享值,请将println调用移动到主类的递增和递减函数:
public static void increment() {
semaphore.down();
sharedValue++;
System.out.println(sharedValue);
semaphore.up();
}
public static void decrement() {
semaphore.down();
sharedValue--;
System.out.println(sharedValue);
semaphore.up();
}
还要注意,即使进行了这些更改,您也不会看到1和0之间的切换。这是因为两个线程不会同时启动,即使它们启动了(例如使用)您无法控制调度,因此它们的进度会有所不同。如果您确实希望观察此输出,则应该让每个线程在调用semaphore.up()之前和之后等待1ms,以便让另一个线程有机会等待并从该信号获取许可
public static void increment() {
semaphore.down();
sharedValue++;
System.out.println(sharedValue);
try {
Thread.sleep(1); //give time to other threads to wait for permit
semaphore.up();
Thread.sleep(1); //give time to other threads to acquire permit
} catch (InterruptedException ex) {
}
}
从两个线程中获得此类输出的方法有很多,但我不想对代码进行重大修改。并缩进代码。您解释了要实现的目标,但没有提到当前代码存在的问题。那么您的问题是什么?:-)抱歉,问题是我不知道如何编辑代码来交替递增和递减共享值,因此它在1和0之间切换。我是否需要使用循环或布尔值来存储一个线程是否已完成。这确实很有用,但在我实现它时,它并没有完美地工作。它在1和0之间切换,用于大多数o但在接近尾声时,它有点出错,我得到了0 1 2 3 2 3 2 1 0。你知道这可能是为什么吗?我刚刚试过几次测试,现在我意识到它有时是有效的,但大多数时候它停止在1和0之间切换,出于某种原因,它会在递减之前递增几倍,你能帮我修复它吗?对不起,我没有o垃圾邮件,但我刚刚编辑了我的代码,将等待时间增加到5毫秒,每次都有效。这是解决问题的一种糟糕方法吗?有没有更有效的方法?再次感谢。