synchronized

synchronized

 package com.tester;

public class Thread3 {

    class Inner {

        private void m4t1() {

            int i = 5;

            while (i– > 0) {

                System.out.println(Thread.currentThread().getName()

                        + ” : Inner.m4t1()=” + i);

                try {

                    Thread.sleep(500);

                } catch (InterruptedException ie) {

                }

            }

        }

        private void m4t2() {

            int i = 5;

            while (i– > 0) {

                System.out.println(Thread.currentThread().getName()

                        + ” : Inner.m4t2()=” + i);

                try {

                    Thread.sleep(500);

                } catch (InterruptedException ie) {

                }

            }

        }

//会同步,同一个对象只有一把锁

        // private synchronized void m4t2() {

        // int i = 5;

        // while (i– > 0) {

        // System.out.println(Thread.currentThread().getName()

        // + ” : Inner.m4t2()=” + i);

        // try {

        // Thread.sleep(500);

        // } catch (InterruptedException ie) {

        // }

        // }

        // }

    }

    private void m4t1(Inner inner) {

        synchronized (inner) { // 使用对象锁

            inner.m4t1();

        }

    }

    private void m4t2(Inner inner) {

        inner.m4t2();

    }

    // 不会同步,因为锁的对象不同

//    private synchronized void m4t2(Inner inner) {

//        inner.m4t2();

//    }

    public static void main(String[] args) {

        final Thread3 myt3 = new Thread3();

        final Inner inner = myt3.new Inner();

        Thread t1 = new Thread(new Runnable() {

            public void run() {

                myt3.m4t1(inner);

            }

        }, “t1”);

        Thread t2 = new Thread(new Runnable() {

            public void run() {

                myt3.m4t2(inner);

            }

        }, “t2”);

        t1.start();

        t2.start();

    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/100540.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pytorch中tensor转numpy

    pytorch中tensor转numpycputensor转numpy:#假定a为tensora.numpy()gputensor转numpy:gpu下的tensor不能直接转numpy,需要先转到cputensor后再转为numpya.cpu().numpy()注:若tensor带有梯度,以上述方式转换时会报错:RuntimeError:Can’tcallnumpy()onTensorthatrequiresgrad.Usetensor.detach().numpy()instead.

    2022年10月19日
    2
  • ubuntu安装python pip_anaconda pipenv

    ubuntu安装python pip_anaconda pipenv简单介绍在Ubuntu上安装Python的virtualenv前提:本机安装了Linux系统(这里推荐用Ubuntu安装,或是CentOS)(Ubuntu的安装比较简单,推荐用VirtualBox,可以去Oracle官网下载VirtualBox(https://www.virtualbox.org/),然后去Ubuntu官网(https://www.ubuntu.com/download)…

    2022年8月26日
    4
  • UIAutomator2的使用教程

    UIAutomator2的使用教程uiautomator2是一个python库,用于Android的UI自动化测试,其底层基于Googleuiautomator,Google提供的uiautomator库可以获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作。

    2022年7月21日
    61
  • ubuntu20.04清华源_ubuntu20.04更换国内源

    ubuntu20.04清华源_ubuntu20.04更换国内源Ubuntu22.04的稳定版计划于2022年4月21日发布。开发工作已经在紧锣密鼓地进行,它将遵循如下发布时间表:2022年2月24日:功能冻结2022年3月17日:用户界面冻结2022年3月31日:测试版发布2022年4月14日:候选版本2022年4月21日:最终稳定版本Ubuntu22.04仍在积极开发中。您不应该在生产机器或主系统上使用它。如果你想在备用机器或虚拟机上测试它,你可以从Ubuntu的网站下载每日

    2025年10月12日
    2
  • Linux安装JDK

    Linux安装JDKLinux安装JDK

    2022年4月22日
    38
  • 配置静态路由,动态路由,默认路由模式_默认路由为网络和掩码

    配置静态路由,动态路由,默认路由模式_默认路由为网络和掩码一、什么是路由路由(routing)是指分组从源到目的地时,决定端到端路径的网络范围的进程[1]。路由工作在OSI参考模型第三层——网络层的数据包转发设备。路由器通过转发数据包来实现网络互连。虽然路由器可以支持多种协议(如TCP/IP、IPX/SPX、AppleTalk等协议),但是在我国绝大多数路由器运行TCP/IP协议。路由器通常连接两个或多个由IP子网或点到点协议标识的…

    2022年9月16日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号