以下两个例子说明synchronized块的用法: (视频下载) (全部书籍)
例1.9.4_a-本章源码
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);
。。。。。。。。。。。。。。。。。