不可变对象的同步(在java中) [英] Synchronization on immutable objects (in java)

查看:188
本文介绍了不可变对象的同步(在java中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码段 - 1

class RequestObject implements Runnable
{
    private static Integer nRequests = 0;

    @Override
    public void run()
    {       
        synchronized (nRequests)
        {
            nRequests++;
        }
    }
}

代码段 - 2

public class Racer implements Runnable
{
    public static Boolean won = false;    

    @Override
    public void run()
    {
        synchronized (won)
        {
            if (!won)
            won = true;
        }
    }        
}

我遇到了竞争条件使用第一个代码段。我理解这是因为我在不可变对象(Integer类型)上获得锁定。

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


  1. 线程1锁定一个对象(比如A),指向 won

  2. 线程2现在试图锁定 won 所指向的对象,并进入A
  3. $的等待队列b $ b
  4. 线程1进入同步块,验证A是否为假并通过说 won = true 创建一个新对象(比如说B)(A认为它赢得了比赛)。

  5. 'won'现在指向B.线程1释放对象A的锁定(不再指向赢得

  6. 现在,在对象A的等待队列中的thread-2被唤醒并获得对象A的锁定,该对象仍然是 false (不可改变地)。它现在进入同步块并假设它也赢了,这是不正确的。

  1. Thread 1 gets a lock on an object(say A) pointed by won
  2. Thread 2 tries to now get a lock on the object pointed to by won and gets in the wait queue for A
  3. 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).
  4. 'won' now points to B. Thread 1 releases the lock on object A(no longer pointed to by won)
  5. 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;
    }

这里有一个短暂的竞争条件,你没有注意到它,因为它去了在第一次执行 run 方法之后离开。之后, 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.

这并不是说你应该在实际项目中编写这样的代码。应将所有锁定对象分配给 final 变量,以确保它们永远不会更改。

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中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆