当前位置:网站首页>涨姿势了!原来这才是多线程正确实现方式
涨姿势了!原来这才是多线程正确实现方式
2022-08-04 11:31:00 【InfoQ】
Java内存模型
线程同步
锁概述
锁的作用
锁的相关概念
可重入性:一个线程持有该锁的时候能够再次/多次申请该锁
锁的争用与调度
锁的粒度
内部锁:Synchronized
Synchronized(对象锁){ 同步代码块,可以在同步代码块中访问共享数据}
Synchronized同步代码块
public class SynchronizedLock { public static void main(String[] args) { SynchronizedLock synchronizedLock=new SynchronizedLock(); for (int i = 0; i <2 ; i++) { new Thread(new RunnableThread()) { @Override public void run() { synchronizedLock.mm(); } }.start(); } } public void mm() { for (int i = 0; i <100 ; i++) { System.out.println(Thread.currentThread().getName()+"-->"+i); } }}
public class SynchronizedLock { public static void main(String[] args) { SynchronizedLock synchronizedLock=new SynchronizedLock(); for (int i = 0; i <2 ; i++) { new Thread(new RunnableThread()) { @Override public void run() { synchronizedLock.mm();//使用锁的对象是synchronizedLock对象 } }.start(); } } public void mm() { synchronized (this)//this作为当前对象 { for (int i = 0; i <100 ; i++) { System.out.println(Thread.currentThread().getName()+"-->"+i); } } }}
锁对象不同不能实现同步
public class SynchronizedLock { public static void main(String[] args) { SynchronizedLock synchronizedLock=new SynchronizedLock(); SynchronizedLock synchronizedLock2=new SynchronizedLock(); new Thread(new RunnableThread()) { @Override public void run() { synchronizedLock.mm();//使用锁的对象是synchronizedLock对象 } }.start(); new Thread(new RunnableThread()) { @Override public void run() { synchronizedLock2.mm();//使用锁的对象是synchronizedLock对象 } }.start(); } public void mm() { synchronized (this)//this作为当前对象 { for (int i = 0; i <100 ; i++) { System.out.println(Thread.currentThread().getName()+"-->"+i); } } }}
使用常量作为锁对象
public class SynchronizedLock { public static void main(String[] args) { SynchronizedLock synchronizedLock=new SynchronizedLock(); SynchronizedLock synchronizedLock2=new SynchronizedLock(); new Thread(new RunnableThread()) { @Override public void run() { synchronizedLock.mm();//使用锁的对象是synchronizedLock对象 } }.start(); new Thread(new RunnableThread()) { @Override public void run() { synchronizedLock.mm();//使用锁的对象是synchronizedLock对象 } }.start(); } public static final Object obj=new Object(); public void mm() { synchronized (obj)//常量作为当前对象 { for (int i = 0; i <100 ; i++) { System.out.println(Thread.currentThread().getName()+"-->"+i); } } }}
同步实例方法
public class SynchronizedLock { public static void main(String[] args) { SynchronizedLock synchronizedLock=new SynchronizedLock(); new Thread(new RunnableThread()) { @Override public void run() { synchronizedLock.mm(); } }.start(); new Thread(new RunnableThread()) { @Override public void run() { synchronizedLock.mm2(); } }.start(); } //同步实例方法 public synchronized void mm() { for (int i = 0; i <100 ; i++) { System.out.println(Thread.currentThread().getName()+"-->"+i); } } public void mm2() { synchronized (this)//常量作为当前对象 { for (int i = 0; i <100 ; i++) { System.out.println(Thread.currentThread().getName()+"-->"+i); } } }}
同步静态方法
public class SynchronizedLock { public static void main(String[] args) { SynchronizedLock synchronizedLock=new SynchronizedLock(); new Thread(new RunnableThread()) { @Override public void run() { synchronizedLock.mm2(); } }.start(); new Thread(new RunnableThread()) { @Override public void run() { SynchronizedLock.mm();//使用锁的对象是SynchronizedLock.class } }.start(); } //同步静态方法 public synchronized static void mm() { for (int i = 0; i <100 ; i++) { System.out.println(Thread.currentThread().getName()+"-->"+i); } } public void mm2() { synchronized (SynchronizedLock.class)//常量作为当前对象 { for (int i = 0; i <100 ; i++) { System.out.println(Thread.currentThread().getName()+"-->"+i); } } }}
同步代码块和同步方法如何选择
public class SynchronizedLock { public static void main(String[] args) { SynchronizedLock synchronizedLock=new SynchronizedLock(); new Thread(new RunnableThread()) { @Override public void run() { try { synchronizedLock.mm2(); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); new Thread(new RunnableThread()) { @Override public void run() { try { synchronizedLock.mm2();//使用锁的对象是SynchronizedLock.class } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); } //同步实例方法 锁的粒度粗 执行效率低 public synchronized void mm() throws InterruptedException { long starttime= System.currentTimeMillis(); System.out.println("start"); Thread.sleep(3000); for (int i = 0; i <100 ; i++) { System.out.println(Thread.currentThread().getName()+"-->"+i); } System.out.println("end"); long Endtime= System.currentTimeMillis(); System.out.println(Endtime-starttime); } //同步代码块 锁的粒度细 并发效率高 public void mm2() throws InterruptedException { System.out.println("start"); Thread.sleep(3000); synchronized (this)//常量作为当前对象 { for (int i = 0; i <100 ; i++) { System.out.println(Thread.currentThread().getName()+"-->"+i); } } System.out.println("end"); }}
脏读
public class Test06 { public static void main(String[] args) throws InterruptedException { User user=new User(); SubThread subThread=new SubThread(user); subThread.start(); user.GetName(); } static class SubThread extends Thread { public User user; public SubThread(User user) { this.user=user; } @Override public void run() { user.SetValue("ww","456"); } } static class User { private String name="ylc"; private String pwd="123"; public void GetName() { System.out.println(Thread.currentThread().getName()+"==>"+name+"密码"+pwd); } public void SetValue(String name,String pwd) { System.out.println("原来为为name="+this.name+",pwd="+this.pwd); this.name=name; this.pwd=pwd; System.out.println("更新为name="+name+",pwd="+pwd); } }}
线程出现异常释放锁
public class SynchronizedLock { public static void main(String[] args) { SynchronizedLock synchronizedLock=new SynchronizedLock(); new Thread(new RunnableThread()) { @Override public void run() { synchronizedLock.mm(); } }.start(); new Thread(new RunnableThread()) { @Override public void run() { synchronizedLock.mm2(); } }.start(); } //同步实例方法 public synchronized void mm() { for (int i = 0; i <100 ; i++) { if(i==50) { Integer.parseInt("abc");//异常设置 } System.out.println(Thread.currentThread().getName()+"-->"+i); } } public void mm2() { synchronized (this) { for (int i = 0; i <100 ; i++) { System.out.println(Thread.currentThread().getName()+"-->"+i); } } }}
死锁
public class Text06_5 { public static void main(String[] args) { SubThread subThread=new SubThread(); SubThread subThread2=new SubThread(); subThread.setName("a"); subThread2.setName("b"); subThread.start();subThread2.start(); } static class SubThread extends Thread { private static final Object lock1=new Object(); private static final Object lock2=new Object(); @Override public void run() { if("a".equals(Thread.currentThread().getName())) { synchronized (lock1) { System.out.println("a 线程 lock1获得了锁,再需要获得lock2"); synchronized (lock2) { System.out.println("a 线程 lock2获得了锁"); } } } if("b".equals(Thread.currentThread().getName())) { synchronized (lock2) { System.out.println("b 线程 lock2获得了锁,再需要获得lock1"); synchronized (lock1) { System.out.println(" b 线程 lock1获得了锁"); } } } } }}
边栏推荐
猜你喜欢
中介者模式(Mediator)
今天15:00 | CVPR 2022 论文分享精彩继续
记我的第一篇CCF-A会议论文|在经历六次被拒之后,我的论文终于中啦,耶!
【目标检测】yolov3特征提取网络------Darknet53网络及pytorch实现
The sword refers to the Great Wall Cannon?Official spy photos of Changan's new pickup
知道创宇EDR系统实力通过中国信通院端点检测与响应产品能力评测
多行函数;group_by分组;having分组后筛选;单表查询总结
使用函数
ECCV 2022 | 清华&腾讯AI Lab提出REALY: 重新思考3D人脸重建的评估方法
云原生Devops 的实现方法
随机推荐
ESP8266-Arduino编程实例-MQ3酒精传感器驱动
化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)
*iframe*
力扣解法汇总1403-非递增顺序的最小子序列
小程序实战(一)- 骨架屏的应用与实现
ESP8266-Arduino编程实例-APDS-9930环境光和趋近感器驱动
The use of DDR3 (Naive) in Xilinx VIVADO (3) simulation test
音频编辑 合唱
多行函数;group_by分组;having分组后筛选;单表查询总结
记我的第一篇CCF-A会议论文|在经历六次被拒之后,我的论文终于中啦,耶!
*W3C* 标准组织
*SEO*
深度学习------pytorch实现划拳模型训练
从零开始Blazor Server(7)--使用Furion权限验证
【目标检测】yolov3特征提取网络------Darknet53网络及pytorch实现
将博客搬至CSDN
怎么禁止textarea拉伸
God Space - the world's first Web3.0-based art agreement creative platform, broadening the boundaries of multi-art integration
【LeetCode】1403.非递增顺序的最小子序列
小程序实战(三) - head组件的封装与使用