In Groovy, I need to implement an ActiveObject instance called MyCounter so that the following code would pass:
final MyCounter counter = new MyCounter()
counter.incrementBy 10
counter.incrementBy 20
counter.update 'Hello'
assert 35 == counter.value
I have come with the two implementations listed below – none of them works.
1.
@ActiveObject
class MyCounter
{
private int counter = 0
@ActiveMethod
def incrementBy(int value)
{
println "incrementBy $value"
counter += value;
}
@ActiveMethod
def update(String value)
{
println "update $value"
counter += value.size();
}
int getValue()
{
println "getValue"
return counter;
}
}
I suppose that this does not work because the calls to incrementBy don’t block, e.g. the value property and thus the counter variable is in fact accessed before the incrementBy operations finish.
2.
@ActiveObject
class MyCounter
{
private int counter = 0
@ActiveMethod
def incrementBy(int value)
{
println "incrementBy $value"
counter += value;
}
@ActiveMethod
def update(String value)
{
println "update $value"
counter += value.size();
}
@ActiveMethod
int value()
{
println "getValue"
return counter;
}
}
The compiler tells me that:
Non-blocking methods must not return a specific type, use def or void instead
Ok, based on the assumption this is GPars, you should be able to use your first implementation with the following code:
You may need to provide more information as to exactly what you intend to happen with regard to flow of execution to get better info.
You can also change your annotations to be indicate blocking:
See relevent docs for info on any of the above.