简体   繁体   English

Java线程安全递归

[英]Java thread safe recursion

I have nested class CRecursion which has method that do recursion. 我有嵌套类CRecursion,它具有递归的方法。 This CRecursion created in many threads. 这个CRecursion在很多线程中创建。 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. 变量值是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 . 阅读有关线程限制的相关StackOverflow问题以及它对线程安全的影响将有所帮助。

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. CRecursion的构造函数的参数不能跨多个线程共享。 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. 例程不能访问任何其他共享存储区域(Java调用堆栈除外)以在调用之间交换数据。
  1. Are you sharing any data between threads? 你在线程之间共享任何数据吗?

If answer to the above question is NO, then the call is implicitly thread safe. 如果对上述问题的回答为NO,那么该调用是隐式线程安全的。

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. 从技术上讲,您仍然可以共享数据并保证线程安全,唯一的条件是对共享数据的所有访问都必须是读取操作。

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. 这就是关键字synchronized的用途......您可以同步一个块/代码块,这样在任何给定时间只能执行一个块。 Of course, there is usually a speed trade-off for this. 当然,通常会有速度权衡。

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM