Java程序设计 多线程【三】
2022/1/25 9:34:22
本文主要是介绍Java程序设计 多线程【三】,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Java程序设计 多线程【三】
同步与死锁
同步
线程同步是指若干个线程对象并行进行资源访问时实现的资源处理的保护操作
演示:卖货
未同步情况:
package org.test; class Test implements Runnable{ private int h = 5; @Override public void run() { while (true){ if(this.h>0){ try{ Thread.sleep(100); }catch (InterruptedException e){ e.printStackTrace(); } this.h=this.h-1; System.out.println(Thread.currentThread().getName()+"卖出一件,剩余"+this.h+"件"); }else { System.out.println("没货了"); break; } } } } public class Hello { public static void main(String[] args) throws Exception{ Test t=new Test(); new Thread(t,"A").start(); new Thread(t,"B").start(); new Thread(t,"C").start(); } }
实现同步处理
Java中提供有synchronized关键字以实现同步处理,同步的关键是要为代码加上“锁”,而对于锁的操作程序有两种:同步代码块、同步方法。
同步代码块
package org.test; class Test implements Runnable{ private int h = 5; @Override public void run() { while (true){ synchronized (this){ if(this.h>0){ try{ Thread.sleep(100); }catch (InterruptedException e){ e.printStackTrace(); } this.h=this.h-1; System.out.println(Thread.currentThread().getName()+"卖出一件,剩余"+this.h+"件"); }else { System.out.println("没货了"); break; } } } } } public class Hello { public static void main(String[] args) throws Exception{ Test t=new Test(); new Thread(t,"A").start(); new Thread(t,"B").start(); new Thread(t,"C").start(); } }
当进行多个线程并发执行时,只允许有一个线程执行此部分代码,就实现了同步处理操作
同步方法
同步代码块可以直接定义在某个方法中,使得方法的部分操作进行同步处理,但是如果现在某一个方法中的全部操作都需要进行同步处理,则可以采用同步方法的形式进行定义,即在方法声明上使用synchronized关键字即可
package org.test; class Test implements Runnable{ private int h = 5; @Override public void run() { while (this.s()) { ; } } public synchronized boolean s() { if (this.h > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } this.h = this.h - 1; System.out.println(Thread.currentThread().getName() + "卖出一件,剩余" + this.h + "件"); return true; } else { System.out.println("没货了"); return false; } } } public class Hello { public static void main(String[] args) throws Exception{ Test t=new Test(); new Thread(t,"A").start(); new Thread(t,"B").start(); new Thread(t,"C").start(); } }
死锁
死锁指两个线程都在等待对方先完成,造成了程序的停滞状态
class A{ public synchronized void t(B b){ System.out.println("你给我a 我就给你b"); b.get(); } public synchronized void get(){ System.out.println("已获取"); } } class B{ public synchronized void t(A a){ System.out.println("你给我b 我就给你a"); a.get(); } public synchronized void get(){ System.out.println("已获取"); } } public class Hello implements Runnable{ private A a =new A(); private B b =new B(); public void DeadLock(){ new Thread(this).start(); a.t(b); } @Override public void run() { b.t(a); } public static void main(String[] args) { new Hello().DeadLock(); } }
死锁一旦出现程序将进入等待状态并且不会向下继续执行
这篇关于Java程序设计 多线程【三】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南
- 2024-09-30Dnd-Kit学习:新手快速入门指南