监视对象在同步块中有什么作用? [英] What effect does the monitor object have in synchronized block?

查看:103
本文介绍了监视对象在同步块中有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过数小时的阅读,我仍在努力了解监视对象的确切功能.

After hours of reading i am still struggling to understand what the monitor object exactly does.

演示我的意思的演示:

public class Demo {

    public static Bathroom bathroom = new Bathroom();

    public static Kitchen kitchen = new Kitchen();

    public static void main(String[] args) {
        (new Thread(new Roommate("bob"))).start();
        (new Thread(new Roommate("john"))).start();
        (new Thread(new Mom())).start();
    }
}

class Bathroom {
    public void use(String who) {
        synchronized (Demo.kitchen) {
            System.out.println(who + " is using bathroom");
            Long time = System.currentTimeMillis();
            while (System.currentTimeMillis() - time < 2000) {
                continue;
            }
            System.out.println(who + " unlocked bathroom");
        }
    }
}

class Kitchen {
    synchronized public void use(String who) {
        System.out.println(who + " is using kitchen");
    }
}

class Roommate implements Runnable {
    private String name;
    public Roommate (String name) { this.name = name; }
    @Override
    public void run() {
        Demo.bathroom.use(name);
    }   
}

class Mom implements Runnable {
    @Override
    public void run() {
        Demo.kitchen.use("mom");
    }   
}

我将'Demo.kitchen'放入了Bathroom的同步块的括号中.如果有人在使用浴室,则浴室和厨房均被锁定.为什么他们都被锁住了?

I put 'Demo.kitchen' into parentheses in Bathroom's synchronized block. If anyone is using the bathroom then both the bathroom and the kitchen are locked. Why are they both locked?

我猜是:

1)监视对象本身(在我的情况下为厨房)已锁定到所有线程(在同步块中使用时)

1) The monitor object itself (the kitchen in my case) becomes locked to all threads (while it is used in synchronized block)

2)如果它们都具有相同的监视对象(在我的情况下,两个室友都具有与监视对象相同的浴室),则一次只允许一个线程执行同步块.

2) Only one thread at a time is allowed to execute the synchronized block if they all have the same monitor object (in my case both roommates had the same bathroom as monitor object)

Marko Topolnik,谢谢您的回答.

Marko Topolnik, thank you for the answer.

我想我知道现在发生了什么.

I think i understand what is happening now.

第一个线程1(鲍勃)开始使用浴室并锁定厨房(因为提供了Demo.kitchen). 当约翰也想使用洗手间时,他不能,不是因为洗手间是锁着的,而是因为他正在检查厨房是否被锁上了(鲍勃只是把它锁了). 妈妈不能使用厨房,因为那是实际被锁住的物体.

First thread1 (bob) starts using the bathroom and locks the kitchen instead (because Demo.kitchen is provided). When john also wants to use bathroom he can't, not because the bathroom is locked, but because he is checking wether the kitchen is locked or not (and bob just locked it). And mom can't use the kitchen, because that is the object which was actually locked.

推荐答案

请注意不要将监视器与它们所属的对象混淆.通常,在考虑监视器和锁时,最好完全忘记对象"方面.

Be careful not to confuse monitors with the objects they belong to. Generally, it is best to entirely forget about the "object" aspect when thinking about monitors and locks.

尤其是,声明了一段特定代码的类与所获取的锁无关.当您将Demo.kitchen锁定在Bathroom的方法内时,只有kitchen对象被锁定.此外,锁定Demo.kitchen不会阻止其他代码同时访问和更改kitchen对象-除非该代码也受同一锁定保护.

Especially, the class where a certain piece of code is declared has nothing to do with the locks being acquired. When you lock Demo.kitchen inside a Bathroom's method, only the kitchen object is locked. Also, locking Demo.kitchen will not prevent other code to concurrently access and change the kitchen object—unless that code is also protected by the same lock.

这篇关于监视对象在同步块中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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