1、this monitor:在成员方法上的synchronized,就是this monitor,等价于在方法中使用synchronized(this)
2、class monitor:在静态方法上的synchronized,就是class monitor,等价于在静态方法中使用synchronized(xxx.class)
实例
public class main { public synchronized void method1(){ system.out.println(thread.currentthread().getname()+ method1); try{ timeunit.minutes.sleep(5); }catch (interruptedexception e){ e.printstacktrace(); } } public synchronized void method2(){ system.out.println(thread.currentthread().getname()+ method2); try{ timeunit.minutes.sleep(5); }catch (interruptedexception e){ e.printstacktrace(); } } public static void main(string[] args) throws interruptedexception { main m = new main(); new thread(m::method1).start(); new thread(m::method2).start(); }}
以上就是java中有什么特殊monitor的详细内容。
