# Package com.singularsys.jep.misc.threadsafeeval

package com.singularsys.jep.misc.threadsafeeval

Classes to allow a simple method of evaluation in multiple threads. Each thread has its own ThreadSafeEvaluator and VariableTable, but all threads can share the same expression. This differs from the technique which uses a SerializableExpression to create a copy of the expression for each thread. The disadvantage of this method is that evaluation is somewhat slower by around 5%. On the plus side construction time is quicker as a new expression does not need to be construct for each thread.

To set up the thread safe evaluation:

       Jep baseJep = new Jep(new ThreadSafeEvaluator());
       baseJep.getOperatorTable().getAssign().setPFMC(new ThreadSafeAssign());
       baseJep.getOperatorTable().getEle().setPFMC(new ThreadSafeEle());

A Jep instance for a new thread can be created with

       Jep childJep = new Jep(new LightWeightComponentSet(baseJep));

A expression can be parsed in the base jep and evaluated in either. Both jep instances have their own variable tables and the variable values are independent.

       Node node = baseJep.Parse("y=x^2");
       baseJep.addVariable("x",2.0);
       childJep.addVariable("x",3.0);
       Object res1 = baseJep.evaluate(node); // result is 4
       Object res2 = childJep.evaluate(node); // result is 9
       Object val1 = baseJep.getVariableValue("y"); // result is 4
       Object val2 = childJep.getVariableValue("y"); // result is 9

Since:
  Jep 3.5
See Also:
  - threads
  - `LightWeightComponentSet`
  - `SerializableExpression`

- Related Packages
  Package
  Description
  com.singularsys.jep.misc
  Miscellaneous components.
- Classes
  Class
  Description
  ThreadSafeAssign
  An assignment operator so we can do x=3+4.
  ThreadSafeEle
  Thread safe version of the element operation.
  ThreadSafeEvaluator
  An evaluator which can safely evaluate an expression in multiple threads.
  ThreadSafeRandom
  Random number generator using the java.util.concurrent.ThreadLocalRandom `nextDouble(double)` method.