Maruthi Shanmugam
Maruthi Shanmugam

Reputation: 346

Groovy - Object reuse.

In the following code, I am creating the groovy instances and caching it in a hashmap.

  GroovyClassLoader loader = new GroovyClassLoader(this.getClass().getClassLoader());
  Class groovyClass = loader.parseClass(groovyMap.get(key), key + ".groovy");
  groovyObject = (GroovyObject) groovyClass.newInstance();
  instanceMap.put(key, groovyObject);

When I get the instances from the cache map in multithreaded mode and do

 groovtIObject.invokeMethod("methodname",args);

Will that method be thread safe , like the same way in java. I dont have any instance level variables shared in groovy script.

Your inputs are appreciated.

Upvotes: 3

Views: 537

Answers (1)

blackdrag
blackdrag

Reputation: 6508

As Groovy produces bytecode and actually generates a method, it will have about the same level of thread-safety as a Java method. Just be aware that the Binding is by default not really threadsafe. But since you said, you exclude instance level variables, it should be fine.

Upvotes: 1

Related Questions