简体   繁体   English

从外部获取线程变量的值

[英]Getting value of a thread variable from outside

Lets say i have a thread running like this: 可以说我有一个像这样运行的线程:

private boolean working = true;

@Override public void run() {
   working = true;
   // do something
   working = false;
   ....
}

and in my main Thread i'm constantly putting out the state of working with 在我的主线程中,我不断推出与之合作的状态

while(threadClassObject.isWorking()) {
    System.out.println(threadClassObject.isWorking());
}

would this work? 这会有用吗? I tried this example and it seems to work. 我试过这个例子,它似乎工作。 But is there a way that this could crash? 但有没有办法可以崩溃? What eg happens if the thread is in the process of changing working while at the exact same time the mainThread tries to read it? 如果线程正在改变工作的过程中,例如在mainThread尝试读取它的同时发生了什么?

The ans to your question is that it might be working but the above code is a risky code and can break any day. 您的问题的答案是它可能正在工作,但上面的代码是一个有风险的代码,可以在任何一天打破。 Try making working volatile like 试着做working volatile

private volatile boolean working = true;

What eg happens if the thread is in the process of changing working while at the exact same time the mainThread tries to read it?

Assignment operation is an atomic operation. 赋值操作是原子操作。 So if you have single cpu, two threads can never collide while accessing the variable. 因此,如果您有单个cpu,则两个线程在访问变量时永远不会发生冲突。 If you have more than one cpu, two threads can collide while accessing the variable. 如果您有多个cpu,则两个线程在访问变量时可能会发生冲突。 For both cases, volatile will make sure that the value is visible to other threads. 对于这两种情况, volatile将确保该值对其他线程可见。

NOTE: volatile is good in your situation but if you have more complex data to share across thread try looking into this 注意: volatile在你的情况下是好的,但如果你有更复杂的数据要跨线程共享,请尝试查看

Edit: 编辑:

Adding the comment also part of the soln. 添加评论也是soln的一部分。 to make it more clear. 使它更清楚。

Basically bcoz of optimization at cpu level, values changed my one thread might not be visible to another. 基本上在cpu级别优化的bcoz,值改变了我的一个线程可能对另一个线程不可见。 A good example is cpu cache bcoz optimization the values are never reflected in ram where the other thread might be reading. 一个很好的例子是cpu cache bcoz optimization,这些值永远不会反映在另一个线程可能正在读取的ram中。 Volatile tells that this variable's value can be changed outside the scope of current thread so no such optimization is done... Volatile告诉我这个变量的值可以在当前线程的范围之外更改,所以不进行这样的优化......

You might also want look into doing something with your "runaway" while loop in your main thread: 您可能还希望在主线程中使用“失控”循环执行某些操作:

The current implementation will keep spinning, not leaving much CPU time left for your threads executing run() (or anyone else in the system, for that matter). 当前的实现将继续旋转,不会为线程执行run() (或系统中的任何其他人run()留下太多的CPU时间。

You'll need to either use synchronization or AtomicBoolean in order for that to be thread safe. 您需要使用同步或AtomicBoolean才能使其成为线程安全的。 What you have in your code will seem to work, but it's possible that when you check the boolean, it won't be the correct value, so you'll see unexpected results. 您的代码中的内容似乎有效,但是当您检查布尔值时,它可能不是正确的值,因此您将看到意外的结果。

No, it is not necessary to lock primitive types like a boolean. 不,没有必要像布尔一样锁定原始类型。 If 'working' was an object, then you'd need to use synchronized blocks. 如果'working'是一个对象,那么你需要使用synchronized块。

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

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