类似于 Java 中的 Rendez-Vous,但不起作用

发布于 2024-12-28 06:20:31 字数 1167 浏览 6 评论 0原文

我在使用 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 main(String[] args) {
        Rdv rdv = new Rdv(4);

        new Runnable() {
            public void run() {
                A a = new A(rdv);
                a.someMethod();
            }
        }.run();

        rdv.add();
        rdv.add();
        rdv.add();
        rdv.add();
    }
}

这个想法是等到 4 个线程告诉“嘿,我完成了”,然后再运行 someMethod() 的末尾。 但是,尽管有 notifyAll(),但 wait() 会永远持续下去。

我不明白怎么办

I have some troubles using wait() and notify(). I need to have a kind of rendez-vous.

Here is the thing, in a small piece of code:

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 main(String[] args) {
        Rdv rdv = new Rdv(4);

        new Runnable() {
            public void run() {
                A a = new A(rdv);
                a.someMethod();
            }
        }.run();

        rdv.add();
        rdv.add();
        rdv.add();
        rdv.add();
    }
}

The idea is to wait until 4 threads tell "hey, i'm done" before to run the end of someMethod().
But the wait() lasts forever, despite of the notifyAll().

I don't get how

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

内心旳酸楚 2025-01-04 06:20:31

wait()notify() 并不意味着直接使用,而是更好的库用于低级同步的原语。

您应该使用更高级别的并发机制,例如 CountDownLatch。您可能希望使用值为 4 的 CountDownLatch。让每个线程调用闩锁的 countDown() 方法,以及您想要等待的线程调用await()。

private CountDownLatch rendezvousPoint = new CountDownLatch(4);

//wait for threads
rendezvousPoint.await();

//do stuff after rendezvous

//in the other 4 threads:
rendezvousPoint.countDown();

wait() and notify() are not meant to be used directly but rather are primitives that better libraries use for low-level synchronization.

You should use a higher-level concurrency mechanism, such as CountDownLatch. You would want to use a CountDownLatch with a value of 4. Have each of the threads call the countDown() method of the latch, and the one you want to wait to call await().

private CountDownLatch rendezvousPoint = new CountDownLatch(4);

//wait for threads
rendezvousPoint.await();

//do stuff after rendezvous

//in the other 4 threads:
rendezvousPoint.countDown();
枕花眠 2025-01-04 06:20:31

嗯...我是唯一一个注意到您实际上没有启动任何线程的人吗?

    new Runnable() {
        public void run() {
            A a = new A(rdv);
            a.someMethod();
        }
    }.run();

应该是

 Thread t = new Thread(
    new Runnable() {
        public void run() {
            A a = new A(rdv);
            a.someMethod();
        }
    });
 t.start();

,如果你想让 4 个线程等待,则应该执行 4 次。

Um... am I the only one noticing that you are not actually starting any threads?

    new Runnable() {
        public void run() {
            A a = new A(rdv);
            a.someMethod();
        }
    }.run();

should be

 Thread t = new Thread(
    new Runnable() {
        public void run() {
            A a = new A(rdv);
            a.someMethod();
        }
    });
 t.start();

And this should be executed 4 times if you want to have 4 threads waiting.

许你一世情深 2025-01-04 06:20:31

不要自己搞乱 wait()notify() ,而是考虑使用 CountDownLatch

Instead of messing with wait() and notify() yourself, consider using a CountDownLatch

幸福丶如此 2025-01-04 06:20:31

当然达不到,因为A类的每个实例都有一个Rdv类的不同实例;

您需要在 A 类中使用 RDV 作为静态变量

Of course it won't be reached, because every instance of class A has a different instance of class Rdv;

You need to use RDV as a static variable in class A

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文