小编dea*_*iop的帖子

Java中的一种Rendez-Vous无法正常工作

我有一些麻烦使用wait()notify().我需要有一种表达方式.

这是一件事,只需一小段代码:

class A {
    private Rdv r;

    public A(Rdv r) {
        this.r = r;
    }

    public void someMethod() {
        synchronized(r) {
            r.wait();
        }

        // ***** some stuff never reached*****
    }
}

class Rdv { 
    private int added;
    private int limit;

    public Rdv(int limit) {
        this.added = 0;
        this.limit = limit;
    }

    public void add() {
        this.added++;

        if(this.added == this.limit) {
            synchronized(this) {
                this.notifyAll();
            }
        }
    }
}

class Main {
    public static void …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading notify wait

2
推荐指数
1
解决办法
1612
查看次数

标签 统计

concurrency ×1

java ×1

multithreading ×1

notify ×1

wait ×1