java中线程同步Synchronized,监视器monitor和锁lock的关系是什
2017-04-12 12:56
399 查看
既然有关监视器monitor的概念比较难,大家怎么解释的都有。首先我给出一下java的官方文档,也是最权威的解释:
Synchronizationis built around an internal entity known as the intrinsic lock ormonitor lock. (The API specification often refers to this entity simplyas a "monitor."),Every object has an intrinsic lock associated with it.By convention, a thread that needs
exclusive and consistent access toan object's fields has to acquire the object's intrinsic lock beforeaccessing them, and then release the intrinsic lock when it's done withthem.
源自:http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html
根据官方文档,我来说说我的看法。马克-to-win: synchronized工作机制是这样的:Java中每个对象都有一把锁与之相关联,锁控制着对象的synchronized代码。一个要执行对象的synchronized代码的线程必须先获得那个对象的锁。有点儿监控(monitor)的感觉吗?
synchronized关键字使用方式有两种:synchronized方法和synchronized块。以下会给出例子:
例1.9.3-本章源码class A {
public synchronized void f1() {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
System.out.println("i = " + i);
}
}
}
class MyThread extends Thread {
A a;
public MyThread(A a) {
this.a = a;
}
public void run() {
a.f1();
}
}
public class TestMark_to_win {
public static void main(String[] args) {
A a = new A();。。。。。。。。。。。。。。。。。详情请进:http://www.mark-to-win.com/JavaBeginner/JavaBeginner6_web.html#relationshipOfSynchronizedLockandmonitor
Synchronizationis built around an internal entity known as the intrinsic lock ormonitor lock. (The API specification often refers to this entity simplyas a "monitor."),Every object has an intrinsic lock associated with it.By convention, a thread that needs
exclusive and consistent access toan object's fields has to acquire the object's intrinsic lock beforeaccessing them, and then release the intrinsic lock when it's done withthem.
源自:http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html
根据官方文档,我来说说我的看法。马克-to-win: synchronized工作机制是这样的:Java中每个对象都有一把锁与之相关联,锁控制着对象的synchronized代码。一个要执行对象的synchronized代码的线程必须先获得那个对象的锁。有点儿监控(monitor)的感觉吗?
synchronized关键字使用方式有两种:synchronized方法和synchronized块。以下会给出例子:
例1.9.3-本章源码class A {
public synchronized void f1() {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
System.out.println("i = " + i);
}
}
}
class MyThread extends Thread {
A a;
public MyThread(A a) {
this.a = a;
}
public void run() {
a.f1();
}
}
public class TestMark_to_win {
public static void main(String[] args) {
A a = new A();。。。。。。。。。。。。。。。。。详情请进:http://www.mark-to-win.com/JavaBeginner/JavaBeginner6_web.html#relationshipOfSynchronizedLockandmonitor
相关文章推荐
- 多线程拨号Java版(探讨synchronized和Lock线程同步)
- Java 多线程(四)——线程同步(synchronized、ReentrantLock)
- java线程之线程同步与线程通信(Synchronized/Lock)
- 【Java基础之线程同步(二)】java线程同步:synchronized关键字,Lock接口以及可重入锁ReentrantLock
- Java 多线程(四)——线程同步(synchronized、ReentrantLock)
- java并发编程:线程安全-线程同步-synchronized和lock
- Java 多线程(四)——线程同步(synchronized、ReentrantLock)
- synchronized和Java.util.concurrent.locks.Lock的异同
- 白话并发冲突与线程同步(2)——Monitor、lock和死锁
- 白话并发冲突与线程同步(2)——Monitor、lock和死锁
- c# 线程同步: 详解lock,monitor,同步事件和等待句柄以及mutex
- Java线程同步 (synchronized wait notify)
- c# 线程同步: 详解lock,monitor,同步事件和等待句柄以及mutex
- c# 线程同步: 详解lock,monitor,同步事件和等待句柄以及mutex (转)
- c# 线程同步: 详解lock,monitor,同步事件和等待句柄以及mutex
- 白话并发冲突与线程同步(2)——Monitor、lock和死锁
- c# 线程同步: 详解lock,monitor,同步事件和等待句柄以及mutex
- c# 线程同步: 详解lock,monitor,同步事件和等待句柄以及mutex
- synchronized 和 java.util.concurrent.locks.Lock 的异同?
- 线程同步作业(一):Lock,monitor