新的java / android

编程入门 行业动态 更新时间:2024-10-25 19:30:47
新的java / android - 了解线程不一致的状态(new to java/android - understanding thread inconsistent state)

我正在阅读本教程 ,它提到如果我们不同步这两行代码就会发生“不一致状态”。 我试图重现这种“不一致的状态”,但我无法这样做。 你能告诉我究竟什么是“不一致的状态”吗?

教程说,“myColorInt的值与myColorName的值不匹配”..但我似乎没有任何问题...你能解释他们试图避免的问题究竟是什么?

I was going through this tutorial and it mentioned that an "inconsistent state" will happen if we do not synchronize these 2 lines of code. I tried to reproduce this "inconsistent state" but I was not able to do so. Can you tell me exactly what is a "inconsistent state" ?

The tutorial stated, "the value of myColorInt won't match the value of myColorName".. but I did not seem to have any problem... can you explain what exactly is the problem they are trying to avoid?

最满意答案

试图避免的情况被称为竞争条件

http://en.wikipedia.org/wiki/Race_condition

序列的结果

int myColorInt = color.getRGB(); //Statement 1 String myColorName = color.getName(); //Statement 2

取决于在其他线程中执行的此代码线程的时序 。 如果另一个线程在这两个语句之间设置颜色,则存储在myColorName的名称将与myColorName存储的颜色不匹配。 这是教程意味着不一致的状态

同步用于对跨线程共享和访问可变数据的方式提供某些保证。 这个街区

synchronized (color) { int myColorInt = color.getRGB(); String myColorName = color.getName(); }

使用同步来确保两个语句以原子方式执行; 也就是说,防止另一个线程在这个线程位于两个语句之间时改变color对象的状态(假设其他线程也正在使用同步)。

即使对于专家来说,并发和线程安全对于获得正确性也是一项挑战。 并发问题导致难以诊断,难以重现和零星的问题。 由于您刚刚开始,我建议尽可能避免并发编程。 否则,Brian Goetz等人的Java Concurrency in Practice一书。 人。 是一个很好的开始。

The situation that is trying to be avoided is known as a race condition:

http://en.wikipedia.org/wiki/Race_condition

The result of the sequence

int myColorInt = color.getRGB(); //Statement 1 String myColorName = color.getName(); //Statement 2

depends on the timing with respect to this thread of code executing in other threads. If another thread sets the color in between these two statements, then the name stored in myColorName won't match the color stored in myColorInt. This is what the tutorial means by inconsistent state.

Synchronization is used to make certain guarantees about how mutable data is shared and accessible across threads. This block

synchronized (color) { int myColorInt = color.getRGB(); String myColorName = color.getName(); }

uses synchronization to make sure the two statements are executed atomically; that is to say, to prevent another thread from changing the state of the color object while this thread is between the two statements (presuming that other threads are also using synchronization properly).

Concurrency and thread safety can be challenging to get correct, even for experts. Concurrency issues result in hard to diagnose, hard to reproduce, and sporadic problems. Since you're just starting out, I recommend avoiding concurrent programming for the time being if possible. Otherwise, the book Java Concurrency in Practice by Brian Goetz, et. al. is an excellent start.

更多推荐

本文发布于:2023-08-06 05:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444597.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:java   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!