Synchronization on immutable objects (in java)(不可变对象上的同步(在 java 中))
问题描述
Code snippet - 1
class RequestObject implements Runnable
{
private static Integer nRequests = 0;
@Override
public void run()
{
synchronized (nRequests)
{
nRequests++;
}
}
}
Code snippet - 2
public class Racer implements Runnable
{
public static Boolean won = false;
@Override
public void run()
{
synchronized (won)
{
if (!won)
won = true;
}
}
}
I was having a race condition with the first code snippet. I understood that this was because I was obtaining a lock on an immutable object(of type Integer).
I have written a second code snippet which is again impervious to 'Boolean' being immutable. But this works(no race condition is displayed in an output run). If I have understood the solution to my previous question properly the below is is one possible way in which things can go wrong
- Thread 1 gets a lock on an object(say A) pointed by
won
- Thread 2 tries to now get a lock on the object pointed to by
won
and gets in the wait queue for A - Thread 1 goes into the synchronized block, verifies that A is false and creates a new object(say B) by saying
won = true
(A thinks it won the race). - 'won' now points to B. Thread 1 releases the lock on object A(no longer pointed to by
won
) - Now, thread-2 which was in the wait queue of object A gets woken up and gets a lock on object A which is still
false
(immutably so). It now goes into the synchronized block and assumes that it has also won, which is not correct.
Why is the second code snippet working fine all the time??
synchronized (won)
{
if (!won)
won = true;
}
Here you have a transient race condition which you don't notice because it goes away after the first execution of the run
method. After that the won
variable constantly points to the same instance of Boolean
representing true
, which thus serves properly as a mutex lock.
This is not to say that you should ever write such code in real projects. All lock objects should be assigned to final
variables to make sure they never change.
这篇关于不可变对象上的同步(在 java 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不可变对象上的同步(在 java 中)
基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01