以下两个例子说明synchronized块的用法:
例1.9.4
class A {
public void disp() {
System.out.println("新线程马克-to-win启动:");
for (int i = 0; i < 10; i++) {
System.out.println(i);
try {
Thread.sleep(500);
} catch (Exception e) {
}
}
}
}
public class TestMark_to_win extends Thread {
A a;
public TestMark_to_win(A a) {
this.a = a;
}
public void run() {
a.disp();
}
public static void main(String[] args) {
A a = new A();
TestMark_to_win t1 = new TestMark_to_win(a);
TestMark_to_win t2 = new TestMark_to_win(a);
t1.start();
t2.start();
}
}
更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44639795/article/details/103095628
原文:https://www.cnblogs.com/xiaolongxia1922/p/14707541.html