我看到一個Java示例,其主方法標記爲synchronized,調用另一個靜態同步方法。其效果是,基本上,只有在main方法返回後,另一個方法纔會在單獨的線程上運行。聲明主要方法同步
這樣的結構有什麼實用功能?
public class SynchronisedMain {
public static synchronized void main(String[] args) throws InterruptedException {
new Thread(new Runnable() {
@Override
public void run() {
thingy();
}
}).start();
System.out.println("Kickstarted thingy thread.");
TimeUnit.MILLISECONDS.sleep(1000);
}
public static synchronized void thingy() {
System.out.println("Thingy!");
}
}
當然有人設計。如果這是「真正」需要的,請將該方法拖入具有不同名稱的新方法中。 – tjg184