用一对标志实现忙等待

时间:2013-02-02 20:34:01

标签: busy-waiting

我正在尝试使用2个标志来实现繁忙的等待机制。我陷入了僵局,但却无法理解为什么......它看起来好像应该有效......

抱歉长码,这是我成功的最短时间。

package pckg1;

public class MainClass {

public static void main(String[] args) {
    Buffer b = new Buffer();
    Producer prod = new Producer(b);
    Consumer cons = new Consumer(b);

    cons.start();
    prod.start();
}
}

class Producer extends Thread {
private Buffer buffer;

public Producer(Buffer buffer1) {
    buffer = buffer1;
}

public void run() {
    for (int i = 0; i < 60; i++) {
        while (!buffer.canUpdate)
            ;
        buffer.updateX();
        buffer.canUpdate = false;
        buffer.canUse = true;
    }
}
}

class Consumer extends Thread {
private Buffer buffer;

public Consumer(Buffer buffer1) {
    buffer = buffer1;
}

public void run() {
    for (int i = 0; i < 60; i++) {
        while (!buffer.canUse)
            ;
        buffer.consumeX();
        buffer.canUse = false;
        buffer.canUpdate = true;
    }
}
}

class Buffer {
private int x;
public boolean canUpdate;
public boolean canUse;

public Buffer() {
    x = 0;
    canUpdate = true;
}

public void updateX() {
    x++;
    System.out.println("updated to " + x);

}

public void consumeX() {
    System.out.println("used " + x);
}
}

1 个答案:

答案 0 :(得分:1)

我建议所有关于Buffer的逻辑应该进入该类。

此外,如果2个或更多个标志可以访问它们,则必须保护访问(和修改)标志。这就是我将synchronised放到2种方法中的原因。

class Buffer {
private int x;
private boolean canUpdate;
private boolean canUse;

public Buffer() {
    x = 0;
    canUpdate = true;
}

public synchronised void updateX() {
    x++;
    System.out.println("updated to " + x);
    canUpdate = false;
    canUse = true;
}

public synchronised void consumeX() {
    System.out.println("used " + x);
    canUpdate = true;
    canUse = false;
}

    public synchronised boolean canUse() {
        return canUse;
    }
    public synchronised boolean canUpdate() {
        return canUpdate;
    }
}

此外,从canUpdatecanUse类中删除ProducerConsumer次写入,并使用方法替换读取(在条件中)。

此外,在等待循环中引入一些Thread.sleep(100)会很有用。