这是我的测试代码,表明您是正确的,并且本文有点过分谨慎:
class Y { static synchronized void staticSleep() { System.out.println("Start static sleep"); try { Thread.sleep(2000); } catch (InterruptedException e) { } System.out.println("End static sleep"); } synchronized void instanceSleep() { System.out.println("Start instance sleep"); try { Thread.sleep(200); } catch (InterruptedException e) { } System.out.println("End instance sleep"); }}public class X { public static void main(String[] args) { for (int i = 0; i < 2; ++i) { new Thread(new Runnable() { public void run() { Y.staticSleep(); } }).start(); } for (int i = 0; i < 10; ++i) { new Thread(new Runnable() { public void run() { new Y().instanceSleep(); } }).start(); } }}印刷品:
Start instance sleepStart instance sleepStart instance sleepStart instance sleepStart instance sleepStart static sleepStart instance sleepStart instance sleepStart instance sleepStart instance sleepStart instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd static sleepStart static sleepEnd static sleep
因此与实例
static synchronized的
synchronized方法无关…
当然,如果
static synchronised整个系统都使用这些方法,那么您可以期望它们对多线程系统的吞吐量产生最大的影响,因此请自担风险。



