package concurrent;
import java.util.concurrent.TimeUnit;
public class ThisMonitor {
public synchronized void method1() {
System.out.println(Thread.currentThread().getName() + " enter to method1");
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " exit to method1");
}
public synchronized void method2() {
System.out.println(Thread.currentThread().getName() + " enter to method2");
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " exit to method2");
}
public static void main(String[] args) {
ThisMonitor thisMonitor = new ThisMonitor();
new Thread(thisMonitor::method1, "T1").start();
new Thread(thisMonitor::method2, "T2").start();
}
}
T1 enter to method1
T1 exit to method1
T2 enter to method2
T2 exit to method2
package concurrent;
import java.util.concurrent.TimeUnit;
public class ThisMonitor1 {
public synchronized void method1() {
System.out.println(Thread.currentThread().getName() + " enter to method1");
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " exit to method1");
}
public void method2() {
synchronized (this) {
System.out.println(Thread.currentThread().getName() + " enter to method2");
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " exit to method2");
}
}
public static void main(String[] args) {
ThisMonitor1 thisMonitor = new ThisMonitor1();
new Thread(thisMonitor::method1, "T1").start();
new Thread(thisMonitor::method2, "T2").start();
}
}
T1 enter to method1
T1 exit to method1
T2 enter to method2
T2 exit to method2
上面两组代码执行的效果是一样的,使用 synchronized 关键字同步类的不同实例方法,争抢的是同一个 monitor 的 lock,而与之关联的引用则是 ThisMonitor 实例的引用。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/chengqiuming/article/details/123262517
内容来源于网络,如有侵权,请联系作者删除!