本文介绍了Java 中的监视器是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 并发编程中的监视器指的是什么?

What's a monitor referred to in concurrent programming in Java?

当我读到每个对象都关联了一个监视器"是什么意思?

When I read that "every object has associated a monitor" what does it meaning?

是特殊物品吗?

推荐答案

监视器是控制对象并发访问的机制.

A monitor is mechanism to control concurrent access to an object.

这允许您执行以下操作:

This allows you to do:

主题 1:

public void a()
{
    synchronized(someObject) {
        // do something (1)
    }
}

主题 2:

public void b()
{
    synchronized(someObject) {
        // do something else (2)
    }
}

这可以防止线程 1 和 2 同时访问受监控(同步)部分.一个将启动,并且监视器将在第一个完成之前阻止另一个访问该区域.

This prevents Threads 1 and 2 accessing the monitored (synchronized) section at the same time. One will start, and monitor will prevent the other from accessing the region before the first one finishes.

这不是一个特殊的对象.它的同步机制位于类层次结构根:java.lang.Object.

It's not a special object. It's synchronization mechanism placed at class hierarchy root: java.lang.Object.

还有 waitnotify 方法也将使用对象的监视器在不同线程之间进行通信.

There are also wait and notify methods that will also use object's monitor to communication among different threads.

这篇关于Java 中的监视器是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 10:32