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?
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:
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?