I have nested class CRecursion which has method that do recursion.
This CRecursion created in many threads. Is safe call from thread method from main class?
Thanks.
class A {
method1() {....}
for(int i=0;i<100;i++){
execute(new CRecursion(...))
}
protected CRecursion {
calculate (par){
if (some_condition) {
calculate(par1)
} else {
String s=method1(value);
.....
}
}
....
}
Variable value is Object. But internal for each method.
If the objects used by the recursive routine are confined to the same thread, then yes, the recursive routine is thread-safe. It would help to read this related StackOverflow question on thread confinement and it’s impact on thread-safety.
In this particular case (with the code that you’ve posted), you’ll need to ensure that:
CRecursionmust not be shared across multiple threads. If they are shared, then the following point becomes relevant.