简体   繁体   English

多个线程访问的方法相同

[英]Same method accessed by multiple threads

I am having a method that is accessed by multiple threads in the java program.This method have: 我有一个方法可以被java程序中的多个线程访问。这个方法有:

  1. Global object variables 全局对象变量
  2. Local variable 局部变量
  3. Formal Parameters. 形式参数。

Now this method is not synchronized so when the multiple threads changes the value of above three types of variables then will these changes reflected in other threads or each thread will have separate copy of these variables. 现在这个方法不同步,因此当多个线程更改上述三种类型的变量的值时,这些更改将反映在其他线程中,或者每个线程将具有这些变量的单独副本。

Global object : Will reflect changes 全局对象 :将反映变化

Local variable : Will not 局部变量 :不会

Formal Parameters : Object references Will reflect changes and parameter of primitive type will not reflect changes. 形式参数 :对象引用将反映基元类型的变化和参数不会反映变化。

Some of the other answers here forget to mention a very important fact. 这里的其他一些答案忘记提到一个非常重要的事实。

Changes to objects passed as parameters and global variables are shared with other threads, however: 作为参数传递的对象的更改和全局变量与其他线程共享,但是:

Without synchronization you will never know when (perhaps never!) these changes will be shown in other threads. 如果没有同步,您将永远不知道何时(可能永远不会!)这些更改将在其他线程中显示。 Besides allowing only one thread in the method at the same time preventing 100 flavors of race conditions, entering and exiting a synchronized block will also trigger all caches to be cleared and data to be shared amongst threads/cores. 除了同时允许方法中的一个线程防止100种竞争条件之外,进入和退出同步块还将触发要清除的所有高速缓存并且在线程/核之间共享数据。

Multithreading is extremely hard to get right, read up on it carefully. 多线程很难做到正确,仔细阅读。 I recommend 'Java Concurrency in Practice' by Goetz. 我推荐Goetz的“Java Concurrency in Practice”。

Golbal objects will be changed, Local vars will not Parameters can be changed if they are objects, if they are primitive types like integer they will not be. Golbal对象将被更改,Local vars将不会参数可以更改,如果它们是对象,如果它们是原始类型,如整数,他们将不会。 Even if they are objects they might not change if every thread creates them just before calling the method and they are not reused. 即使它们是对象,如果每个线程在调用方法之前创建它们并且它们不被重用,它们也可能不会改变。

But that's not all, since you cannot control the order of execution they will change randomly even when one thread is processing so if you have say a global variable int myVar =0; 但这不是全部,因为你无法控制执行的顺序,即使一个线程正在处理它们也会随机改变,所以如果你说一个全局变量int myVar = 0;

and the method does 而且方法可以

{  // line 10
        myVar = 1;  // line 11
        myVar += 7;  // line 12
    }  // line 13

there is no gurantee at all that the variable will be 1 at the beginning of line 12 since some other thread might have changed the value in the time it takes the program to go from line 11 to line 12. 由于某些其他线程可能已经改变了程序从第11行到第12行所花费的时间内的值,因此根本没有保证变量在第12行的开头是1。

Bottom line this is a recipe for dissaster. 底线这是一个骗局的秘诀。 You need to either synchronize the method or add a lock to the global variables and the objects you pass as parameters. 您需要同步方法或将锁定添加到全局变量以及作为参数传递的对象。

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

相关问题 对多个线程同时访问的静态最终方法的困惑 - confusion over static final method accessed by multiple threads at the same time Java多线程可访问的静态方法 - Static method to be accessed by multiple threads, Java 可以单独访问的多个线程 - Multiple Threads that can be accessed separately 由多个线程访问的Singleton类中的实例变量 - Instance variable in a Singleton class accessed by multiple threads 如何避免从具有相同参数的多个线程执行方法? - How to avoid method execution from multiple threads with same parameter? 如果多个线程访问相同的privarte方法,变量值会混合吗? - If Multiple threads access same privarte method, will the variables value get mixed? 使用数据库DAO类方法获取的相同值的多个线程 - multiple threads using same value fetched by database DAO class method 如何在 Spring 中运行相同的@Scheduled 方法以多线程启动 - How to run the same @Scheduled method in Spring Boot in multiple threads 多个线程执行相同的方法(非同步)行为 - Multiple Threads executing same Method (Non Synchronized) behavior Java中有多个线程访问同一方法时的线程安全 - Thread safety when multiple threads accessing the same method in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM