【Java基础】多线程之synchronized

2024-08-26 22:38

本文主要是介绍【Java基础】多线程之synchronized,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

为什么要用synchronized


          多线程可以提高效率,但是在共享变量的情况下,可能出现混乱的情况,比如A线程将共享变量 m=5,但是B线程在用m变量的时候,出现了混乱。这样多个线程共同访问同样的一个资源,你争我夺,一团乱。线程的不安全现象一触即发。所以必须要对这种资源冲突进行预防。

      所以,在共同访问的资源进行加锁即可,第一种方法就是同步锁,也即用关键字synchronized。该关键字可以加在方法和语句块中,也即是同步方法和同步语句块。

synchronized原理

           各个线程在调用关键字synchronized方法或者语句块的时候,需要排队进行。当A线程在调用该方法(或者语句块)的时候,要在该方法(或者语句块)上锁,B线程则等待,当然可以以异步的方法调用非synchronized方法。当A线程释放了synchronized方法(或者语句块),B线程才可以调用该方法。
    
       synchronized关键字还可以实现可重入锁,也就是一个线程连续几次获取同一把锁。

案例分析


      首先是方法类,代码如下:

package cn.itcast.java.base.synchronize;public class Task
{public void doLongTimeTask(){for (int i = 0; i < 100; i++){System.out.println("nosynchronized threadName="+Thread.currentThread().getName()+"i="+(i+1));}System.out.println("");synchronized (this){for (int i = 0; i < 100; i++){System.out.println("synchronized threadName="+Thread.currentThread().getName()+"i="+(i+1));}}}
}

      接下来是自定义的线程类,这里一共有2个线程:

package cn.itcast.java.base.synchronize;public class MyThread1 extends Thread
{private Task task;public MyThread1(Task task){super();this.task=task;}public void run(){super.run();task.doLongTimeTask();}}

package cn.itcast.java.base.synchronize;public class MyThread2 extends Thread
{private Task task;public MyThread2(Task task){super();this.task=task;}public void run(){super.run();task.doLongTimeTask();}}


    接下来是run方法:

    
package cn.itcast.java.base.synchronize;public class Run
{public static void main(String[] args)
{Task task=new Task();MyThread1 thread1=new MyThread1(task);thread1.start();MyThread2 thread2=new MyThread2(task);thread2.start();
}
}
   
     我们看一下运行结果:

     
nosynchronized threadName=Thread-0i=1
nosynchronized threadName=Thread-1i=1
nosynchronized threadName=Thread-0i=2
nosynchronized threadName=Thread-1i=2
nosynchronized threadName=Thread-0i=3
nosynchronized threadName=Thread-1i=3
nosynchronized threadName=Thread-0i=4
nosynchronized threadName=Thread-1i=4
nosynchronized threadName=Thread-0i=5
nosynchronized threadName=Thread-1i=5
nosynchronized threadName=Thread-0i=6
nosynchronized threadName=Thread-1i=6
nosynchronized threadName=Thread-0i=7
nosynchronized threadName=Thread-0i=8
nosynchronized threadName=Thread-0i=9
nosynchronized threadName=Thread-1i=7
nosynchronized threadName=Thread-0i=10
nosynchronized threadName=Thread-1i=8
nosynchronized threadName=Thread-0i=11
nosynchronized threadName=Thread-0i=12
nosynchronized threadName=Thread-0i=13
nosynchronized threadName=Thread-1i=9
nosynchronized threadName=Thread-0i=14
nosynchronized threadName=Thread-1i=10
nosynchronized threadName=Thread-0i=15
nosynchronized threadName=Thread-1i=11
nosynchronized threadName=Thread-0i=16
nosynchronized threadName=Thread-0i=17
nosynchronized threadName=Thread-0i=18
nosynchronized threadName=Thread-0i=19
nosynchronized threadName=Thread-0i=20
nosynchronized threadName=Thread-0i=21
nosynchronized threadName=Thread-0i=22
nosynchronized threadName=Thread-0i=23
nosynchronized threadName=Thread-0i=24
nosynchronized threadName=Thread-0i=25
nosynchronized threadName=Thread-0i=26
nosynchronized threadName=Thread-0i=27
nosynchronized threadName=Thread-0i=28
nosynchronized threadName=Thread-0i=29
nosynchronized threadName=Thread-1i=12
nosynchronized threadName=Thread-0i=30
nosynchronized threadName=Thread-1i=13
nosynchronized threadName=Thread-0i=31
nosynchronized threadName=Thread-1i=14
nosynchronized threadName=Thread-0i=32
nosynchronized threadName=Thread-1i=15
nosynchronized threadName=Thread-0i=33
nosynchronized threadName=Thread-1i=16
nosynchronized threadName=Thread-0i=34
nosynchronized threadName=Thread-1i=17
nosynchronized threadName=Thread-0i=35
nosynchronized threadName=Thread-1i=18
nosynchronized threadName=Thread-0i=36
nosynchronized threadName=Thread-1i=19
nosynchronized threadName=Thread-0i=37
nosynchronized threadName=Thread-0i=38
nosynchronized threadName=Thread-1i=20
nosynchronized threadName=Thread-0i=39
nosynchronized threadName=Thread-1i=21
nosynchronized threadName=Thread-0i=40
nosynchronized threadName=Thread-1i=22
nosynchronized threadName=Thread-0i=41
nosynchronized threadName=Thread-1i=23
nosynchronized threadName=Thread-0i=42
nosynchronized threadName=Thread-1i=24
nosynchronized threadName=Thread-0i=43
nosynchronized threadName=Thread-1i=25
nosynchronized threadName=Thread-0i=44
nosynchronized threadName=Thread-1i=26
nosynchronized threadName=Thread-0i=45
nosynchronized threadName=Thread-1i=27
nosynchronized threadName=Thread-0i=46
nosynchronized threadName=Thread-1i=28
nosynchronized threadName=Thread-0i=47
nosynchronized threadName=Thread-1i=29
nosynchronized threadName=Thread-0i=48
nosynchronized threadName=Thread-1i=30
nosynchronized threadName=Thread-0i=49
nosynchronized threadName=Thread-0i=50
nosynchronized threadName=Thread-1i=31
nosynchronized threadName=Thread-1i=32
nosynchronized threadName=Thread-1i=33
nosynchronized threadName=Thread-1i=34
nosynchronized threadName=Thread-0i=51
nosynchronized threadName=Thread-1i=35
nosynchronized threadName=Thread-0i=52
nosynchronized threadName=Thread-1i=36
nosynchronized threadName=Thread-0i=53
nosynchronized threadName=Thread-0i=54
nosynchronized threadName=Thread-0i=55
nosynchronized threadName=Thread-0i=56
nosynchronized threadName=Thread-0i=57
nosynchronized threadName=Thread-0i=58
nosynchronized threadName=Thread-0i=59
nosynchronized threadName=Thread-0i=60
nosynchronized threadName=Thread-0i=61
nosynchronized threadName=Thread-0i=62
nosynchronized threadName=Thread-1i=37
nosynchronized threadName=Thread-0i=63
nosynchronized threadName=Thread-1i=38
nosynchronized threadName=Thread-0i=64
nosynchronized threadName=Thread-1i=39
nosynchronized threadName=Thread-0i=65
nosynchronized threadName=Thread-1i=40
nosynchronized threadName=Thread-0i=66
nosynchronized threadName=Thread-1i=41
nosynchronized threadName=Thread-0i=67
nosynchronized threadName=Thread-1i=42
nosynchronized threadName=Thread-0i=68
nosynchronized threadName=Thread-1i=43
nosynchronized threadName=Thread-0i=69
nosynchronized threadName=Thread-1i=44
nosynchronized threadName=Thread-0i=70
nosynchronized threadName=Thread-1i=45
nosynchronized threadName=Thread-0i=71
nosynchronized threadName=Thread-1i=46
nosynchronized threadName=Thread-0i=72
nosynchronized threadName=Thread-1i=47
nosynchronized threadName=Thread-1i=48
nosynchronized threadName=Thread-1i=49
nosynchronized threadName=Thread-1i=50
nosynchronized threadName=Thread-1i=51
nosynchronized threadName=Thread-0i=73
nosynchronized threadName=Thread-1i=52
nosynchronized threadName=Thread-0i=74
nosynchronized threadName=Thread-1i=53
nosynchronized threadName=Thread-0i=75
nosynchronized threadName=Thread-1i=54
nosynchronized threadName=Thread-0i=76
nosynchronized threadName=Thread-0i=77
nosynchronized threadName=Thread-0i=78
nosynchronized threadName=Thread-0i=79
nosynchronized threadName=Thread-0i=80
nosynchronized threadName=Thread-1i=55
nosynchronized threadName=Thread-0i=81
nosynchronized threadName=Thread-0i=82
nosynchronized threadName=Thread-1i=56
nosynchronized threadName=Thread-0i=83
nosynchronized threadName=Thread-1i=57
nosynchronized threadName=Thread-1i=58
nosynchronized threadName=Thread-0i=84
nosynchronized threadName=Thread-1i=59
nosynchronized threadName=Thread-0i=85
nosynchronized threadName=Thread-1i=60
nosynchronized threadName=Thread-0i=86
nosynchronized threadName=Thread-1i=61
nosynchronized threadName=Thread-0i=87
nosynchronized threadName=Thread-1i=62
nosynchronized threadName=Thread-0i=88
nosynchronized threadName=Thread-1i=63
nosynchronized threadName=Thread-0i=89
nosynchronized threadName=Thread-1i=64
nosynchronized threadName=Thread-0i=90
nosynchronized threadName=Thread-1i=65
nosynchronized threadName=Thread-0i=91
nosynchronized threadName=Thread-1i=66
nosynchronized threadName=Thread-0i=92
nosynchronized threadName=Thread-1i=67
nosynchronized threadName=Thread-0i=93
nosynchronized threadName=Thread-1i=68
nosynchronized threadName=Thread-0i=94
nosynchronized threadName=Thread-1i=69
nosynchronized threadName=Thread-0i=95
nosynchronized threadName=Thread-1i=70
nosynchronized threadName=Thread-0i=96
nosynchronized threadName=Thread-1i=71
nosynchronized threadName=Thread-0i=97
nosynchronized threadName=Thread-1i=72
nosynchronized threadName=Thread-0i=98
nosynchronized threadName=Thread-1i=73
nosynchronized threadName=Thread-0i=99
nosynchronized threadName=Thread-1i=74
nosynchronized threadName=Thread-0i=100
nosynchronized threadName=Thread-1i=75nosynchronized threadName=Thread-1i=76
synchronized threadName=Thread-0i=1
nosynchronized threadName=Thread-1i=77
synchronized threadName=Thread-0i=2
nosynchronized threadName=Thread-1i=78
synchronized threadName=Thread-0i=3
synchronized threadName=Thread-0i=4
synchronized threadName=Thread-0i=5
nosynchronized threadName=Thread-1i=79
synchronized threadName=Thread-0i=6
nosynchronized threadName=Thread-1i=80
synchronized threadName=Thread-0i=7
nosynchronized threadName=Thread-1i=81
synchronized threadName=Thread-0i=8
synchronized threadName=Thread-0i=9
synchronized threadName=Thread-0i=10
synchronized threadName=Thread-0i=11
synchronized threadName=Thread-0i=12
synchronized threadName=Thread-0i=13
synchronized threadName=Thread-0i=14
synchronized threadName=Thread-0i=15
synchronized threadName=Thread-0i=16
synchronized threadName=Thread-0i=17
synchronized threadName=Thread-0i=18
synchronized threadName=Thread-0i=19
synchronized threadName=Thread-0i=20
synchronized threadName=Thread-0i=21
synchronized threadName=Thread-0i=22
synchronized threadName=Thread-0i=23
nosynchronized threadName=Thread-1i=82
synchronized threadName=Thread-0i=24
nosynchronized threadName=Thread-1i=83
synchronized threadName=Thread-0i=25
nosynchronized threadName=Thread-1i=84
synchronized threadName=Thread-0i=26
nosynchronized threadName=Thread-1i=85
synchronized threadName=Thread-0i=27
nosynchronized threadName=Thread-1i=86
synchronized threadName=Thread-0i=28
nosynchronized threadName=Thread-1i=87
synchronized threadName=Thread-0i=29
nosynchronized threadName=Thread-1i=88
synchronized threadName=Thread-0i=30
synchronized threadName=Thread-0i=31
synchronized threadName=Thread-0i=32
synchronized threadName=Thread-0i=33
nosynchronized threadName=Thread-1i=89
synchronized threadName=Thread-0i=34
nosynchronized threadName=Thread-1i=90
synchronized threadName=Thread-0i=35
nosynchronized threadName=Thread-1i=91
synchronized threadName=Thread-0i=36
nosynchronized threadName=Thread-1i=92
synchronized threadName=Thread-0i=37
nosynchronized threadName=Thread-1i=93
synchronized threadName=Thread-0i=38
nosynchronized threadName=Thread-1i=94
nosynchronized threadName=Thread-1i=95
nosynchronized threadName=Thread-1i=96
nosynchronized threadName=Thread-1i=97
synchronized threadName=Thread-0i=39
nosynchronized threadName=Thread-1i=98
synchronized threadName=Thread-0i=40
nosynchronized threadName=Thread-1i=99
synchronized threadName=Thread-0i=41
nosynchronized threadName=Thread-1i=100synchronized threadName=Thread-0i=42
synchronized threadName=Thread-0i=43
synchronized threadName=Thread-0i=44
synchronized threadName=Thread-0i=45
synchronized threadName=Thread-0i=46
synchronized threadName=Thread-0i=47
synchronized threadName=Thread-0i=48
synchronized threadName=Thread-0i=49
synchronized threadName=Thread-0i=50
synchronized threadName=Thread-0i=51
synchronized threadName=Thread-0i=52
synchronized threadName=Thread-0i=53
synchronized threadName=Thread-0i=54
synchronized threadName=Thread-0i=55
synchronized threadName=Thread-0i=56
synchronized threadName=Thread-0i=57
synchronized threadName=Thread-0i=58
synchronized threadName=Thread-0i=59
synchronized threadName=Thread-0i=60
synchronized threadName=Thread-0i=61
synchronized threadName=Thread-0i=62
synchronized threadName=Thread-0i=63
synchronized threadName=Thread-0i=64
synchronized threadName=Thread-0i=65
synchronized threadName=Thread-0i=66
synchronized threadName=Thread-0i=67
synchronized threadName=Thread-0i=68
synchronized threadName=Thread-0i=69
synchronized threadName=Thread-0i=70
synchronized threadName=Thread-0i=71
synchronized threadName=Thread-0i=72
synchronized threadName=Thread-0i=73
synchronized threadName=Thread-0i=74
synchronized threadName=Thread-0i=75
synchronized threadName=Thread-0i=76
synchronized threadName=Thread-0i=77
synchronized threadName=Thread-0i=78
synchronized threadName=Thread-0i=79
synchronized threadName=Thread-0i=80
synchronized threadName=Thread-0i=81
synchronized threadName=Thread-0i=82
synchronized threadName=Thread-0i=83
synchronized threadName=Thread-0i=84
synchronized threadName=Thread-0i=85
synchronized threadName=Thread-0i=86
synchronized threadName=Thread-0i=87
synchronized threadName=Thread-0i=88
synchronized threadName=Thread-0i=89
synchronized threadName=Thread-0i=90
synchronized threadName=Thread-0i=91
synchronized threadName=Thread-0i=92
synchronized threadName=Thread-0i=93
synchronized threadName=Thread-0i=94
synchronized threadName=Thread-0i=95
synchronized threadName=Thread-0i=96
synchronized threadName=Thread-0i=97
synchronized threadName=Thread-0i=98
synchronized threadName=Thread-0i=99
synchronized threadName=Thread-0i=100
synchronized threadName=Thread-1i=1
synchronized threadName=Thread-1i=2
synchronized threadName=Thread-1i=3
synchronized threadName=Thread-1i=4
synchronized threadName=Thread-1i=5
synchronized threadName=Thread-1i=6
synchronized threadName=Thread-1i=7
synchronized threadName=Thread-1i=8
synchronized threadName=Thread-1i=9
synchronized threadName=Thread-1i=10
synchronized threadName=Thread-1i=11
synchronized threadName=Thread-1i=12
synchronized threadName=Thread-1i=13
synchronized threadName=Thread-1i=14
synchronized threadName=Thread-1i=15
synchronized threadName=Thread-1i=16
synchronized threadName=Thread-1i=17
synchronized threadName=Thread-1i=18
synchronized threadName=Thread-1i=19
synchronized threadName=Thread-1i=20
synchronized threadName=Thread-1i=21
synchronized threadName=Thread-1i=22
synchronized threadName=Thread-1i=23
synchronized threadName=Thread-1i=24
synchronized threadName=Thread-1i=25
synchronized threadName=Thread-1i=26
synchronized threadName=Thread-1i=27
synchronized threadName=Thread-1i=28
synchronized threadName=Thread-1i=29
synchronized threadName=Thread-1i=30
synchronized threadName=Thread-1i=31
synchronized threadName=Thread-1i=32
synchronized threadName=Thread-1i=33
synchronized threadName=Thread-1i=34
synchronized threadName=Thread-1i=35
synchronized threadName=Thread-1i=36
synchronized threadName=Thread-1i=37
synchronized threadName=Thread-1i=38
synchronized threadName=Thread-1i=39
synchronized threadName=Thread-1i=40
synchronized threadName=Thread-1i=41
synchronized threadName=Thread-1i=42
synchronized threadName=Thread-1i=43
synchronized threadName=Thread-1i=44
synchronized threadName=Thread-1i=45
synchronized threadName=Thread-1i=46
synchronized threadName=Thread-1i=47
synchronized threadName=Thread-1i=48
synchronized threadName=Thread-1i=49
synchronized threadName=Thread-1i=50
synchronized threadName=Thread-1i=51
synchronized threadName=Thread-1i=52
synchronized threadName=Thread-1i=53
synchronized threadName=Thread-1i=54
synchronized threadName=Thread-1i=55
synchronized threadName=Thread-1i=56
synchronized threadName=Thread-1i=57
synchronized threadName=Thread-1i=58
synchronized threadName=Thread-1i=59
synchronized threadName=Thread-1i=60
synchronized threadName=Thread-1i=61
synchronized threadName=Thread-1i=62
synchronized threadName=Thread-1i=63
synchronized threadName=Thread-1i=64
synchronized threadName=Thread-1i=65
synchronized threadName=Thread-1i=66
synchronized threadName=Thread-1i=67
synchronized threadName=Thread-1i=68
synchronized threadName=Thread-1i=69
synchronized threadName=Thread-1i=70
synchronized threadName=Thread-1i=71
synchronized threadName=Thread-1i=72
synchronized threadName=Thread-1i=73
synchronized threadName=Thread-1i=74
synchronized threadName=Thread-1i=75
synchronized threadName=Thread-1i=76
synchronized threadName=Thread-1i=77
synchronized threadName=Thread-1i=78
synchronized threadName=Thread-1i=79
synchronized threadName=Thread-1i=80
synchronized threadName=Thread-1i=81
synchronized threadName=Thread-1i=82
synchronized threadName=Thread-1i=83
synchronized threadName=Thread-1i=84
synchronized threadName=Thread-1i=85
synchronized threadName=Thread-1i=86
synchronized threadName=Thread-1i=87
synchronized threadName=Thread-1i=88
synchronized threadName=Thread-1i=89
synchronized threadName=Thread-1i=90
synchronized threadName=Thread-1i=91
synchronized threadName=Thread-1i=92
synchronized threadName=Thread-1i=93
synchronized threadName=Thread-1i=94
synchronized threadName=Thread-1i=95
synchronized threadName=Thread-1i=96
synchronized threadName=Thread-1i=97
synchronized threadName=Thread-1i=98
synchronized threadName=Thread-1i=99
synchronized threadName=Thread-1i=100
    

          用synchronized(this)来锁定当前对象,通过语句块来进行同步。
     
       
        

  

这篇关于【Java基础】多线程之synchronized的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1109889

相关文章

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis