3

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.

user710818
  • 23,228
  • 58
  • 149
  • 207
  • Are the different calls sharing the same mutable data? Or are they all working on data local to the method itself? – Hovercraft Full Of Eels Sep 23 '11 at 17:21
  • If you have a CPU bound process it it likely that the optimal number of threads to use is the number of cores you have. e.g. if you have 4 cores, use only 4 threads. This may help you determine if this is a good idea or not. ;) – Peter Lawrey Sep 23 '11 at 17:28
  • Additionally, what is best represented using recursion in a functional language is usually best performed as a loop in Java. i.e. much faster. There are occasions where recursion is fastest, but these are relatively rare in Java. – Peter Lawrey Sep 23 '11 at 17:30

3 Answers3

3

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:

  • The arguments to the constructor of CRecursion must not be shared across multiple threads. If they are shared, then the following point becomes relevant.
  • Any objects that are shared across multiple threads, must not be accessed (read from or written to) in the recursion routine.
  • The recursion routine uses local variables that are confined to the current stack frame. The routine must not access any other shared storage area (other than the Java call stack) to exchange data between invocations.
Community
  • 1
  • 1
Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
1
  1. Are you sharing any data between threads?

If answer to the above question is NO, then the call is implicitly thread safe.

If you are worried that local variables will be garbled via multiple call to the same method from different threads, then you are mistaken. Each invocation of a method creates its own seperate copy of those variables.

In essence if you are not sharing any data your call is thread safe.

Technically, you can still share data and be thread safe, the only condition is that all access to the shared data must be a read operation.

Swaranga Sarma
  • 13,055
  • 19
  • 60
  • 93
1

We need more details about your calculate method to answer your question. If you are only using locally scoped variables (ie, variables / data you create within the method) then you are fine.

If you are accessing data within the class, but only reading that data, then you are fine.

If you are accessing data within the class, and are writing to that data, you may have a problem. This is what the keyword synchronized is for... you can synchronize a block / blocks of code so that only one block can be executed at any given time. Of course, there is usually a speed trade-off for this.

Hope that helps.

Eric Lindauer
  • 1,813
  • 13
  • 19