简体   繁体   English

各种java线程访问相同的变量

[英]Various java threads accessing same variable

If I have 10 threads or so accessing the same variable (only reading it, not writing) will java allow these to read the variable simultaneously or each thread will queue for reading?如果我有 10 个左右的线程访问同一个变量(只读取它,不写入),java 会允许这些线程同时读取变量还是每个线程都将排队等待读取?

I am interested on the standard behaviour, ie no special care was taken accessing the variable.我对标准行为感兴趣,即没有特别注意访问变量。

The program is running on a Linux RedHat.该程序在 Linux RedHat 上运行。

Ideally, it will read it simultaneously, but it is not safe in some cases.理想情况下,它会同时读取它,但在某些情况下并不安全。

For example, if you declare:例如,如果您声明:

int i = 1;

Thread threadOne = new Thread() {
public void run() {
    i++;
    System.out.println("ThreadOne is " + i);
}
};

Thread threadTwo = new Thread() {
public void run() {
    i++;
    System.out.println("ThreadTwo is "+ i);
}
};

Thread threadThree = new Thread() {
public void run() {
    i++;
    System.out.println("ThreadThree is " + i);
}
};

threadOne.start();
threadTwo.start();
threadThree.start();

You can try several times and you will see the result varies.您可以尝试多次,您会看到结果各不相同。 Without synchronization, all of thread will read and write the memory "randomly" OR "simultaneously" in the other word, depend on who finish first.如果没有同步,所有线程将“随机”或“同时”读写内存,这取决于谁先完成。

The result I get after run the program several times:多次运行程序后得到的结果:

ThreadOne is 1
ThreadThree is 3
ThreadTwo is 2

ThreadOne is 3
ThreadThree is 3
ThreadTwo is 3

ThreadTwo is 2
ThreadThree is 3
ThreadOne is 2

As we can see, all three thread read the memory which contain int i randomly and update the int a by adding one.如我们所见,所有三个线程都随机读取包含 int i 的内存,并通过添加一个来更新 int a。 If one thread has added one, then another thread will read 2.如果一个线程添加了一个,那么另一个线程将读取 2。

The system.out.println() is also a process. system.out.println() 也是一个进程。 In the second attempt, it prints all 3s.在第二次尝试中,它打印所有 3。 It means after three thread has read int i and add 1, the final result of int i became 3. Then the system.out.println will all print 3. Here is another way to prove, among all these threads, all the process are running simultaneously or the thread are running simultaneously.意思是三个线程读完int i并加1后,int i的最终结果变成了3。那么system.out.println会全部打印3。这里还有一种方法可以证明,在所有这些线程中,所有的进程都是同时运行或线程同时运行。

Multiple threads can read the same value if you allow it without using synchronization.如果您允许而不使用同步,则多个线程可以读取相同的值。 Although if you are not changing the value, make sure that all 10 threads saw the latest value (perhaps the initial value) before the long period of reading (this would require synchronization at least once).尽管如果您不更改该值,请确保在长时间读取之前所有 10 个线程都看到了最新值(可能是初始值)(这至少需要同步一次)。

Yes the read AND write will be done simultaneously, to have more consistency you should add the volatile keyword to your variable.是的,读取和写入将同时完成,为了获得更高的一致性,您应该将volatile关键字添加到您的变量中。 It still don't hold a lock, but if somehow in the future you want to do some write operations that would be more consistent.它仍然不持有锁,但是如果将来您想以某种方式执行一些更一致的写操作。

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

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