And*_*san 4 java concurrency multithreading
我看到一个 Java 示例,其中有一个 main 方法标记为 synchronized,调用另一个静态同步方法。结果是,基本上,其他方法仅在主方法返回后才在单独的线程上运行。
这样的构造有什么实际功能?
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!");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3878 次 |
| 最近记录: |