首页 > 编程语言 > 详细

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

时间:2021-04-27 14:24:04      阅读:14      评论:0      收藏:0      [点我收藏+]

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

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

原文:https://www.cnblogs.com/xiaolongxia1922/p/14707541.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!