Groovy 等待/通知

发布于 2024-11-25 21:04:21 字数 470 浏览 4 评论 0原文

我有以下 Groovy 代码:

abstract class Actor extends Script {
    synchronized void proceed() {
        this.notify()
    }

    synchronized void pause() {
        wait()
    }
}

class MyActor extends Actor {
    def run() {
        println "hi"
        pause()
        println "hi again"
    }
}

def theactor = new MyActor()
theactor.run()
theactor.proceed()

当我运行代码时,我希望代码输出“hi”和“hi Again”。相反,它只是停在“hi”处并卡在pause()函数上。关于如何继续该计划有什么想法吗?

I have the following Groovy code:

abstract class Actor extends Script {
    synchronized void proceed() {
        this.notify()
    }

    synchronized void pause() {
        wait()
    }
}

class MyActor extends Actor {
    def run() {
        println "hi"
        pause()
        println "hi again"
    }
}

def theactor = new MyActor()
theactor.run()
theactor.proceed()

When I run the code, I want the code to output "hi" and "hi again". Instead, it just stops at "hi" and gets stuck on the pause() function. Any idea on how I could continue the program?

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

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

发布评论

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

评论(2

一个人的夜不怕黑 2024-12-02 21:04:21

正如布莱恩所说,多线程和并发是一个巨大的领域,出错比正确更容易......

为了让你的代码正常工作,你需要有这样的东西:

abstract class Actor implements Runnable {
  synchronized void proceed() { notify() }
  synchronized void pause()   { wait()   }
}

class MyActor extends Actor {
  void run() {
    println "hi"
    pause()
    println "hi again"
  }
}


def theactor = new MyActor()             // Create an instance of MyActor
def actorThread = new Thread( theactor ) // Create a Thread to run this instance in
actorThread.start()                      // Thread.start() will call MyActor.run()
Thread.sleep( 500 )                      // Make the main thread go to sleep for some time so we know the actor class is waiting
theactor.proceed()                       // Then call proceed on the actor
actorThread.join()                       // Wait for the thread containing theactor to terminate

但是,如果如果你正在使用 Groovy,我会认真考虑使用 像 Gpars 这样的框架,它为 Groovy 带来了并发性,并且是由人们编写的谁真正了解他们的东西。然而,我想不出任何允许这种任意暂停代码的东西......也许你可以设计你的代码来适应他们的使用模式之一?

As Brian says, multithreading and concurrency is a huge area, and it is easier to get it wrong, than it is to get it right...

To get your code working, you'd need to have something like this:

abstract class Actor implements Runnable {
  synchronized void proceed() { notify() }
  synchronized void pause()   { wait()   }
}

class MyActor extends Actor {
  void run() {
    println "hi"
    pause()
    println "hi again"
  }
}


def theactor = new MyActor()             // Create an instance of MyActor
def actorThread = new Thread( theactor ) // Create a Thread to run this instance in
actorThread.start()                      // Thread.start() will call MyActor.run()
Thread.sleep( 500 )                      // Make the main thread go to sleep for some time so we know the actor class is waiting
theactor.proceed()                       // Then call proceed on the actor
actorThread.join()                       // Wait for the thread containing theactor to terminate

However, if you are using Groovy, I would seriously consider using a framework like Gpars which brings concurrency to Groovy and is written by people who really know their stuff. I can't think of anything that llows this sort of arbitrary pausing of code however... Maybe you could design your code to fit one of their usage patterns instead?

○闲身 2024-12-02 21:04:21

线程是一个很大的话题,Java 中有一些库可以完成许多常见的事情,而无需直接使用 Thread API。 “Fire and Forget”的一个简单示例是 Timer

但要回答你眼前的问题;另一个线程需要通知您的线程继续。请参阅文档 等待()

导致当前线程等待,直到另一个线程调用
该对象的notify() 方法或notifyAll() 方法。在其他方面
换句话说,这个方法的行为就像它只是执行调用一样
等待(0)。

一个简单的“修复”就是为您的等待调用添加固定的持续时间,以便继续您的探索。我推荐这本书“Java 并发实践”。

synchronized void pause() {
        //wait 5 seconds before resuming.
        wait(5000)
    }

Threading is a big topic and there are libraries in Java to do many common things without working with the Thread API directly. One simple example for 'Fire and Forget' is Timer.

But to answer your immediate question; another thread needs to notify your thread to continue. See the docs on wait()

Causes current thread to wait until another thread invokes the
notify() method or the notifyAll() method for this object. In other
words, this method behaves exactly as if it simply performs the call
wait(0).

One simple 'fix' is to just add a fixed duration to your wait call just to continue with your exploration. I would suggest the book 'Java Concurrency in Practice'.

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