java中synchronized块是怎么用的给个例子

以下两个例子说明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);

。。。。。。。。。。。。。。。。。

详情请进:http://www.mark-to-win.com/index.html?content=JavaBeginner/javaUrl.html&chapter=JavaBeginner/JavaBeginner6_web.html#Usage

猜你喜欢

转载自www.cnblogs.com/mark-to-win/p/9697778.html