你不是真正的懂Synchronized

你不是真正的懂Synchronized,你的懂只是你穿的保护色。

前言

Synchronized作为用的比较多的同步工具,经常被我们用到,下面有5道题,
Queston1

Class Test{
       Public void Synchronized foo();
       Public void Synchronized bar();
}
       t1: new Test().foo();
       t2: new Test().foo();

t1和t2 Test对象的foo方法可以同时执行吗?


Queston2

Class Test{
       Public void Synchronized foo();
       Public void Synchronized bar();
}
       Test test = new Test();
       t1: test.foo();
       t2: test.foo();

t1和t2 Test对象的foo方法可以同时执行吗?


Queston3

Class Test{
       Public void Synchronized foo();
       Public void Synchronized bar();
}
       Test test = new Test();
       t1: test.foo();
       t2: test.bar();

t1 Test对象的foo方法和t2 Test对象的bar方法可以同时执行吗?


Queston4

Class Test{
       Public static void Synchronized foo();
       Public void Synchronized bar();
}
       Test test = new Test();
       t1: test.foo();
       t2: test.bar();

t1 Test对象的foo方法和t2 Test对象的bar方法可以同时执行吗?


Queston5

Class Test{
       Public static void Synchronized foo();
       Public void Synchronized bar();
}
       Test test = new Test();
       t1: new Test.foo();
       t2: new Test.foo();

t1和t2 Test对象的foo方法可以同时执行吗?


答案是YNNYN

归结为一句话就是:非静态方法为synchronized(this) ,静态方法为 synchronzied(Class)。

END

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容