Lambdas are not thread safe. Nowhere in the Rules DSL docs or Xtend docs are they claimed to be thread safe. It would be a mistake to assume that anything in the Rules DSL is thread safe.
If you must have a thread safe lambda you have two options, listed in my recommended order of preference:
-
Look for an approach that doesn’t require the lambda in the first place. In my experience, lambdas are a tool that rarely need to be used in Rules as there is almost always a better alternative that avoids many of the pitfalls lambdas bring with them. Design Pattern: Separation of Behaviors, Design Pattern: Associated Items, and applying Design Pattern: Working with Groups in Rules were written to show how to write generic Rules or combinations of Rules that do not require lambdas in the first place,
-
Create multiple instances of the lambda. A lambda is just an Object. So create a pool of th2m to call so each thread get’s it’s own instance of the lambda.
-
Put a lock around the call to the lambda, not inside the lambda itself.
Please do file the issue as this might be something the developers can address, but I suspect the problem lies deep down in the base libraries/languages that the Rules DSL is built upon.