多线程访问同步方法的7种情况

本文详细解析了Java中synchronized关键字的使用场景,包括线程如何访问同步方法、静态同步方法、不同普通同步方法等,以及在异常情况下的锁释放机制。

如果大家想了解synchronized的信息,可以看我的另外一篇博客:https://blog.csdn.net/killerofjava/article/details/103374409
1.两个线程同时访问一个对象的同步方法。

public class SynchronizedObjectMethod3 implements Runnable {
    static SynchronizedObjectMethod3 soMethod=new SynchronizedObjectMethod3();
    @Override
    public void run() {
       method();
    }
    public synchronized  void method(){
        System.out.println("我的对象锁的方法修饰形式,我叫"+Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"运行结束");
    }
    public static void main(String[] args) {
        Thread thread01 = new Thread(soMethod);
        Thread thread02 = new Thread(soMethod);
        thread01.start();
        thread02.start();
        while (thread01.isAlive() || thread02.isAlive()){

        }
        System.out.println("finished");
    }
}

结果:
在这里插入图片描述

原因:因为这两个线程用的都是同一个runnable实例,所以它们拿的是同一把对象锁,既然是同一把锁那么它们自然就窜行执行。

2.两个线程访问的是两个对象的同步方法。

public class SynchronizedObjectMethod3 implements Runnable {
    static SynchronizedObjectMethod3 soMethod1=new SynchronizedObjectMethod3();
    static SynchronizedObjectMethod3 soMethod2=new SynchronizedObjectMethod3();
    @Override
    public void run() {
       method();
    }
    public synchronized  void method(){
        System.out.println("我的对象锁的方法修饰形式,我叫"+Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"运行结束");
    }
    public static void main(String[] args) {
        Thread thread01 = new Thread(soMethod1);
        Thread thread02 = new Thread(soMethod2);
        thread01.start();
        thread02.start();
        while (thread01.isAlive() || thread02.isAlive()){

        }
        System.out.println("finished");
    }
}

结果:
在这里插入图片描述
原因:因为这两个线程用的不是同一个runnable实例,所以它们拿的不是同一把对象锁,既然不是同一把锁,那么它们自然就并行执行。
3.两个线程访问的是synchronized的静态方法

public class SynchronizedClassStatic4 implements  Runnable{
    static SynchronizedClassStatic4 instance1 = new SynchronizedClassStatic4();
    static SynchronizedClassStatic4 instance2 = new SynchronizedClassStatic4();
    @Override
    public void run() {
        method();
    }
    public static  synchronized  void method(){
        System.out.println("我是类锁的第一种形式:static形式,我叫"+Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"运行结束");
    }
    public static void main(String[] args) {
        Thread thread01 = new Thread(instance1);
        Thread thread02 = new Thread(instance2);
        thread01.start();
        thread02.start();
        while (thread01.isAlive() || thread02.isAlive()){

        }
        System.out.println("finished");
    }
}

结果:
在这里插入图片描述
原因:因为访问的方法被synchronized和static关键字修饰,所以不管是不是用的是同一个runnable实例,它们获得的都是同一把类锁,自然是窜行执行。
4.同时访问同步方法和非同步方法

public class SynchronizedYesOrNo6 implements  Runnable {
    static SynchronizedYesOrNo6 instance = new SynchronizedYesOrNo6();
    @Override
    public void run() {
        if ("Thread-0".equals(Thread.currentThread().getName())){
            method1();
        }else{
            method2();
        }
    }
    public synchronized  void method1(){
        System.out.println("我加锁的方法,我叫"+Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"运行结束");
    }

    public   void method2(){
        System.out.println("我没加锁的方法,我叫"+Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"运行结束");
    }
    public static void main(String[] args) {
        Thread thread01 = new Thread(instance);
        Thread thread02 = new Thread(instance);
        thread01.start();
        thread02.start();
        while (thread01.isAlive() || thread02.isAlive()){

        }
        System.out.println("finished");
    }
}

结果:
在这里插入图片描述

原因:因为没有加锁的方法是可以被任何线程访问的,所以它们并行执行。
5.访问同一个对象的不同的普通同步方法。

public class SynchronizedDifferentMethod7 implements Runnable {
    static SynchronizedDifferentMethod7 instance = new SynchronizedDifferentMethod7();
    @Override
    public void run() {
        if ("Thread-0".equals(Thread.currentThread().getName())){
            method1();
        }else{
            method2();
        }
    }
    public synchronized  void method1(){
        System.out.println("我加锁的方法1,我叫"+Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"运行结束");
    }

    public synchronized   void method2(){
        System.out.println("我加锁的方法2,我叫"+Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"运行结束");
    }
    public static void main(String[] args) {
        Thread thread01 = new Thread(instance);
        Thread thread02 = new Thread(instance);
        thread01.start();
        thread02.start();
        while (thread01.isAlive() || thread02.isAlive()){

        }
        System.out.println("finished");
    }
}

结果:
在这里插入图片描述

原因:因为用的是同一个runnable实例,所以这两个普通的同步方法用的都是同一把对象锁,自然它们都是窜行执行。

6.同时访问静态synchronized和非synchronized方法。

public class SynchronizedStaticAndNormal8 implements Runnable {
    static SynchronizedStaticAndNormal8 instance = new SynchronizedStaticAndNormal8();
    @Override
    public void run() {
        if ("Thread-0".equals(Thread.currentThread().getName())){
            method1();
        }else{
            method2();
        }
    }
    public static synchronized  void method1(){
        System.out.println("我静态加锁的方法1,我叫"+Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"运行结束");
    }

    public synchronized   void method2(){
        System.out.println("我非静态加锁的方法2,我叫"+Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"运行结束");
    }
    public static void main(String[] args) {
        Thread thread01 = new Thread(instance);
        Thread thread02 = new Thread(instance);
        thread01.start();
        thread02.start();
        while (thread01.isAlive() || thread02.isAlive()){

        }
        System.out.println("finished");
    }
}

结果:
在这里插入图片描述
原因;因为获得的锁不同,一个获得的是类锁,二另一个获得的是对象锁,所以它们会并行执行。

7.方法抛出异常后,会释放锁。

public class SynchronizedException9 implements Runnable {
    static SynchronizedException9 instance = new SynchronizedException9();
    @Override
    public void run() {
        if ("Thread-0".equals(Thread.currentThread().getName())){
            method1();
        }else{
            method2();
        }
    }
    public  synchronized  void method1(){
        System.out.println("我加锁的方法1,我叫"+Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int i=8/0;
        System.out.println(Thread.currentThread().getName()+"运行结束");
    }

    public synchronized  void method2(){
        System.out.println("我加锁的方法2,我叫"+Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"运行结束");
    }
    public static void main(String[] args) {
        Thread thread01 = new Thread(instance);
        Thread thread02 = new Thread(instance);
        thread01.start();
        thread02.start();
        while (thread01.isAlive() || thread02.isAlive()){

        }
        System.out.println("finished");
    }
}

结果:
在这里插入图片描述
原因:被synchronized关键字修饰的方法在抛出异常后,jvm会自动的帮我们释放锁,不需要我们手动释放锁。我们可以看到在method1()方法抛出异常后,method2()方法立马执行了。

go实战微服务分布式系统(distributed system)是建立在网络之上的软件系统。正是因为软件的特性,所以分布式系统具有高度的内聚性和透明性。因此,网络和分布式系统之间的区别更多的在于高层软件(特别是操作系统),而不是硬件。在一个分布式系统中,一组独立的计算机展现给用户的是一个统一的整体,就好像是一个系统似的。系统拥有多种通用的物理和逻辑资源,可以动态的分配任务,分散的物理和逻辑资源通过计算机网络实现信息交换。系统中存在一个以全局的方式管理计算机资源的分布式操作系统。通常,对用户来说,分布式系统只有一个模型或范型。在操作系统之上有一层软件中间件(middleware)负责实现这个模型。一个著名的分布式系统的例子是万维网(World Wide Web),在万维网中,所有的一切看起来就好像是一个文档(Web页面)一样。 [1] 在计算机网络中,这种统一性、模型以及其中的软件都不存在。用户看到的是实际的机器,计算机网络并没有使这些机器看起来是统一的。如果这些机器有不同的硬件或者不同的操作系统,那么,这些差异对于用户来说都是完全可见的。如果一个用户希望在一台远程机器上运行一个程序,那么,他必须登陆到远程机器上,然后在那台机器上运行该程序。 [1] 分布式系统和计算机网络系统的共同点是:多数分布式系统是建立在计算机网络之上的,所以分布式系统计算机网络在物理结构上是基本相同的。 [1] 他们的区别在于:分布式操作系统的设计思想和网络操作系统是不同的,这决定了他们在结构、工作方式和功能上也不同。网络操作系统要求网络用户在使用网络资源时首先必须了解网络资源,网络用户必须知道网络中各个计算机的功能配置、软件资源、网络文件结构等情况,在网络中如果用户要读一个共享文件时,用户必须知道这个文件放在哪一台计算机的哪一个目录下;分布式操作系统是以全局方式管理系统资源的,它可以为用户任意调度网络资源,并且调度过程是“透明”的。当用户提交一个作业时,分布式操作系统能够根据需要在系统中选择最合适的处理器,将用户的作业提交到该处理程序,在处理器完成作业后,将结果传给用户。在这个过程中,用户并不会意识到有多个处理器的存在,这个系统就像是一个处理器一样。 [1] 内聚性是指每一个数据库分布节点高度自治,有本地的数据库管理系统。透明性是指每一个数据库分布节点对用户的应用来说都是透明的,看不出是本地还是远程。在分布式数据库系统中,用户感觉不到数据是分布的,即用户不须知道关系是否分割、有无副本、数据存于哪个站点以及事务在哪个站点上执行等。  什么是微服务?维基上对其定义为:一种软件开发技术- 面向服务的体系结构(SOA)架构样式的一种变体,将应用程序构造为一组松散耦合的服务。在微服务体系结构中,服务是细粒度的,协议是轻量级的。微服务(或微服务架构)是一种云原生架构方法,其中单个应用程序由许多松散耦合且可独立部署的较小组件或服务组成。这些服务通常● 有自己的堆栈,包括数据库和数据模型;● 通过REST API,事件流和消息代理的组合相互通信;● 和它们是按业务能力组织的,分隔服务的线通常称为有界上下文。尽管有关微服务的许多讨论都围绕体系结构定义和特征展开,但它们的价值可以通过相当简单的业务和组织收益更普遍地理解:● 可以更轻松地更新代码。● 团队可以为不同的组件使用不同的堆栈。● 组件可以彼此独立地进行缩放,从而减少了因必须缩放整个应用程序而产生的浪费和成本,因为单个功能可能面临过多的负载。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值