Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:06:32+00:00 2026-06-15T07:06:32+00:00

In Groovy, I need to implement an ActiveObject instance called MyCounter so that the

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:06:33+00:00Added an answer on June 15, 2026 at 7:06 am

    Ok, based on the assumption this is GPars, you should be able to use your first implementation with the following code:

    final MyCounter counter = new MyCounter()
    def x1 = counter.incrementBy 10
    def x2 = counter.incrementBy 20
    def x3 = counter.update 'Hello'
    x1.get() //block
    x2.get() //block
    x3.get() //block
    assert 35 == counter.value
    

    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:

    @ActiveMethod(blocking=true)
    

    See relevent docs for info on any of the above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in groovy, and I need to control to loop
I need to print XML that I manipulated using groovy and maintain the attribute
In Groovy, how do I extract a new list from the following: def people
I am executing groovy script in java: final GroovyClassLoader classLoader = new GroovyClassLoader(); Class
Eclipse use the same Code formatter for Groovy and Java. But I need different
However strange it sounds I need to modify some Groovy code by modifying how
Why is it that just when I need it, Groovy's metaClass property returns null.
I need to migrate a database to an entirely new schema using groovy scripts,
I have some string with Groovy expression. I need: Execute Groovy expression that is
Ok, this should be easy... I'm new to groovy and I'm looking to implement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.