
说明
1、this monitor:在成员方法上的synchronized,就是this monitor,等价于在方法中使用synchronized(this)
2、class monitor:在静态方法上的synchronized,就是class monitor,等价于在静态方法中使用synchronized(XXX.class)
实例
publicclassMain{
publicsynchronizedvoidmethod1(){
System.out.println(Thread.currentThread().getName()+"method1");
try{
TimeUnit.MINUTES.sleep(5);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
publicsynchronizedvoidmethod2(){
System.out.println(Thread.currentThread().getName()+"method2");
try{
TimeUnit.MINUTES.sleep(5);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args)throwsInterruptedException{
Mainm=newMain();
newThread(m::method1).start();
newThread(m::method2).start();
}
}
以上就是java中特殊monitor的介绍,希望对大家有所帮助。更多Java学习指路:Java基础
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END

















































暂无评论内容