多重锁-幕后 [英] Multiple locks - Behind the scene

查看:139
本文介绍了多重锁-幕后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class A {

Object lock1 = new Object();
Object lock2 = new Object();

List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();

void insert1() {
    synchronized (lock1) {
        list1.add(5);
    }
}

void insert2() {
    synchronized (lock2) {
        list2.add(5);
    }
}

void firing() {
    for (int i = 0; i < 1000000; i++) {
        insert1();
        insert2();
    }
}

以上是代码的一部分,此处线程t1调用firing(),而t2调用firing(). 我使用了lock1和lock2,这样两个线程都不会等待释放锁. 我的问题是幕后发生了什么?

lock1和lock2对象是类A的成员.

lock1和lock2与list1.add(5),list2.add(5)之间是否存在任何关系,它们是否也是lock1和lock2 Object的成员?或仅用于此?.

当使用同步的void insert1()和同步的void insert2()或同步的(this)时,void insert1()和void insert2()是A类的成员,并且获取了A类的锁,这在幕后发生,在上述情况下,创建两个对象只是为了获取不同的锁.

如果我有很多方法,例如void insert1(),这些方法也需要同步,而不是我要创建那么多锁= new Object();那该怎么办? ?

解决方案

synchronized(lock1)仅做一件事情,并且仅做一件事情:它可以防止其他线程同时在同一个对象上进行同步.就是这样.

以下是您使用它的方式和原因:

您有一个具有某种状态的对象(例如,list1),并且您希望通过调用类似list1.add(...)的方法来更新该对象(即,更改其状态). synchronized解决的问题是,像list1.add()这样的方法可能必须将对象置于临时的无效状态中才能生效.您不希望任何线程能够在其他线程对其进行更新的同时查看 list1的状态.

因此,您指定了一个锁定对象(例如,示例中的lock1).并且确保程序中的每个代码块都更新list1甚至看着 list1都在synchronized(lock1){ ... }块内完成

由于没有两个线程可以同时在同一个对象上同步,所以没有两个线程可以同时修改或使用list1.

class A {

Object lock1 = new Object();
Object lock2 = new Object();

List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();

void insert1() {
    synchronized (lock1) {
        list1.add(5);
    }
}

void insert2() {
    synchronized (lock2) {
        list2.add(5);
    }
}

void firing() {
    for (int i = 0; i < 1000000; i++) {
        insert1();
        insert2();
    }
}

Above is an part of code, here thread t1 calls firing() and t2 calls firing(). I used lock1 and lock2 so that both thread will not wait to release the lock . My question is what happens behind the scene ?

lock1 and lock2 object are members of class A.

Is there any relation between lock1 and lock2 with list1.add(5), list2.add(5), Are they also members of lock1 and lock2 Object ? or It is just used insted of this ?.

When used synchronized void insert1() and synchronized void insert2() or synchronized(this), void insert1() and void insert2() are members of class A and lock of class A is acquired ,this happens behind the scenes but in above case two object are created just to acquire different lock.

What if I have many methods like void insert1() which are also needed to be synchronized than should I create that many locks = new Object(); ?

解决方案

synchronized(lock1) Does one thing, and one thing only: It prevents other threads from synchronizing on the same object at the same time. That's all.

Here's how and why you use it:

You have an object (e.g., list1) that has some kind of state, and you wish to update the object (i.e., change its state) by calling a method like list1.add(...). The problem that synchronized solves is, a method like list1.add() may have to put the object into a temporary, invalid state in order to effect the update. You don't want any thread to be able to look at the state of list1 while some other thread is updating it.

So, you designate a lock object (e.g., lock1 in your example). And you make sure that every block of code in your program that either updates list1 or even looks at list1 does it inside a synchronized(lock1){ ... } block.

Since no two threads can synchronize on the same object at the same time, no two threads will be able to modify or use list1 at the same time.

这篇关于多重锁-幕后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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