Are method parameters thread safe in Java?

前端 未结 6 1758
-上瘾入骨i
-上瘾入骨i 2020-12-14 03:24
Class Shared{    
     public void sharedMethod(Object o){
          //does something to Object
     }     
}

//this is how threads call the shared method
run(){
           


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 04:13

    Treat it this way.

    If your threads don't share any common resources, than it's impossible to have concurrency problems. As much as we can tell from the information you provided, the only thing that can be shared here, is someObject. If it's thread-safe itself, or being copied for each thread, than your code is thread safe in general, unless there are other shared resources.

提交回复
热议问题