当前位置: 首页 > ops >正文

【Java】JUC并发(线程的方法、多线程的同步并发)

线程的方法

一、线程的插队:join()方法

1、作用

        暂停当前线程的执行,直到调用join()的目标线程执行完毕,但不影响同一时刻的其他线程。

// 使用join()
public class Test01 {public static void main(String[] args) throws InterruptedException {Thread t = new Thread(()->{System.out.println("子线程 => begin");try {Thread.sleep(500);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("子线程 => end");});t.start();// t线程插队到主线程(当前线程)前面// t线程结束后,主线程才会继续执行// t.join() 相当于 t.join(0 )t.join();System.out.println("主线程 => end");}
}子线程 => begin
子线程 => end
主线程 => end

// 不使用join()
public class Test01 {public static void main(String[] args) throws InterruptedException {Thread t = new Thread(()->{System.out.println("子线程 => begin");try {Thread.sleep(500);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("子线程 => end");});t.start();// t线程插队到主线程(当前线程)前面// t线程结束后,主线程才会继续执行// t.join() 相当于 t.join(0 )
//        t.join();System.out.println("主线程 => end");}
}主线程 => end
子线程 => begin
子线程 => end

2、重载形式

  • join() : 无限等待,直到目标线程终止
  • join(long millis):等待指定毫秒,超时不再等待
  • join(long millis, int nanos) :精确到纳秒,超时不再等待

3、使用场景(多个线程要求按序执行)

public class Test02 {public static void main(String[] args) {Thread t1 = new Thread(()->{for (int i = 0;i<5;i++){System.out.println(Thread.currentThread().getName() + "->" + i);}},"数字线程");Thread t2 = new Thread(()->{try {// 等待t1线程完成t1.join();} catch (InterruptedException e) {throw new RuntimeException(e);}for (char i = 'A';i<'E';i++){System.out.println(Thread.currentThread().getName() + "->" + i);}},"字母线程");Thread t3 = new Thread(()->{try {// 等待t2线程完成t2.join();} catch (InterruptedException e) {throw new RuntimeException(e);}for (char i = 'A';i<'E';i++){System.out.println(Thread.currentThread().getName() + "->" + (char)(Math.random()*100));}},"符号线程");t1.start();t2.start();t3.start();}
}数字线程->0
数字线程->1
数字线程->2
数字线程->3
数字线程->4
字母线程->A
字母线程->B
字母线程->C
字母线程->D
符号线程->I
符号线程->&
符号线程->
符号线程->

4、join()和sleep()的区别

  • join()执行过程中,会释放当前线程的锁;sleep()执行过程中,不会释放当前线程的锁。
  • join()是用过wait/notify机制思想;sleep()通过操作系统的定时器来实现。
  • join()主要实现线程任务编排;sleep()用与模仿耗时操作。

二、线程的中断:interrupt()方法

1、作用

        将该线程的中断状态设置为true,线程的下一步动作取决于中断状态。

2、实现原理

        支持中断的方法(sleep()、join()、wait()等方法)在执行过程中,会监视中断状态,一旦发现其状态为“true”,就会抛出一个中断异常InterruptedException,并给等待状态发出一个中断信号,从而退出等待状态。注意:当线程中没有可以监听中断状态的方法时,interrupt()方法将不起作用。

public class Test03 {public static void main(String[] args) {Thread t = new Thread(()->{// 场景1 : 调用休眠方法(支持中断方法)System.out.println("子线程开始执行!");try {// 子线程休眠4秒Thread.sleep(1000*4);} catch (InterruptedException e) {System.out.println("子线程中断!");return;}System.out.println("子线程结束!");});t.start();try {// 主线程休眠3秒Thread.sleep(1000*3);} catch (InterruptedException e) {throw new RuntimeException(e);}t.interrupt();System.out.println("主线程结束!");}
}当子线程处于休眠状态时,而主线程对子线程进行中断则会子线程中断,不会继续向下执行。子线程开始执行!
主线程结束!
子线程中断!

public class Test03 {public static void main(String[] args) {Thread t = new Thread(()->{// 场景2 : 判断线程的中断状态while (!isinterrupt){System.out.println("子线程开始执行!!");}System.out.println("子线程中断!");});t.start();try {Thread.sleep(1000*3);} catch (InterruptedException e) {throw new RuntimeException(e);}t.interrupt();System.out.println("主线程结束!");}
}当没有使用中断方法时,子线程的中断状态一直时false,while的条件一
直为true会一直执行,当使用了中断方法后,中断状态更新为true,则会
退出while循环继续向下执行。

三、线程的让出:yield()方法

1、作用

        让当前获取CPU的线程,主动让出CPU不执行。

public class demo2 {public static void main(String[] args) {Thread t1 = new Thread(){public void run() {Thread.yield();for (int i = 0;i<5;i++){System.out.println(i);}}};Thread t2 = new Thread(){public void run() {for (char i = 'A';i<'E';i++){System.out.println(i);}}};t1.start();t2.start();}
}

四、守护线程(Daemon Thread)

1、用户线程和守护线程

用户线程:通常为创建的普通线程

守护线程:守护线程执行结束后,虚拟机不会自定退出;而非守护线程执行完毕后,虚拟机会自动退出

2、设置守护线程

        在调用start()方法前,调用setDaemon(true)把该线程标记为守护线程。

Thread t1 = new Thread();
t1.setDaemon(true);
t1.start();

多线程同步并发

一、多线程的数据不一致

        当多个线程同时运行时,线程的调度有操作系统决定,程序本身无法决定。因此,任何一个线程都有可能在任何指令出被系统暂停,然后莫个时间段后继续执行。如果多个线程同时共享一个资源,则会出现数据不一致的问题。

public class Test04 {public static void main(String[] args) {Thread add = new Thread(new AddThread());Thread dec = new Thread(new DecThread());add.start();dec.start();try {add.join();dec.join();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Count.count);}
}
class Count{public static int count = 0; //公共的累加值public static Object lock = new Object(); // 锁对象
}// 累加线程
class AddThread implements Runnable{@Overridepublic void run() {for (int i = 0; i<10000;i++){Count.count += 1;}}}
}
// 累减线程
class DecThread implements Runnable{@Overridepublic void run() {for (int i = 0; i<10000;i++){Count.count -= 1;}}}
}由于算数运算没有原子性所以会出现算数运算被覆盖的情况,从而导致结果不正确
-1234

二、 synchronized关键字

1、什么是synchronized

        Synchronized关键字,也可以理解为一种同步锁。他可以对一段代码进行加锁和解锁,从而使其拥有原子性,从而确保代码的线程安全。

2、synchronized 的用法

  • 修饰实例方法:synchronized 修饰实例方法,则用到的锁,默认为 this 当前方法调用对象;
public class Test05 {public static void main(String[] args) {Foo f1 = new Foo();Foo f2 = new Foo();Thread t1 = new Thread(()->{f1.add();});Thread t2 = new Thread(()->{f1.dec();});t1.start();t2.start();try {t1.join();t2.join();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Foo.counter);}
}
class Foo{public static int counter = 0;public void add(){synchronized (this){for (int i = 0;i<10000;i++){counter++;}}}// 修饰实例方法,使用this关键字做锁public synchronized void dec(){for (int i=0;i<10000;i++){counter--;}}
}
  • 修饰静态方法:synchronized 修饰静态方法,则其所用的锁,默认为 Class 对象;
public class Test05 {public static void main(String[] args) {Foo f1 = new Foo();Foo f2 = new Foo();Thread t1 = new Thread(()->{f1.add();});Thread t2 = new Thread(()->{f1.dec();});t1.start();t2.start();try {t1.join();t2.join();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Foo.counter);}
}
class Foo{public static int counter = 0;public void add(){synchronized (this.getClass()){for (int i = 0;i<10000;i++){counter++;}}}public synchronized static void dec(){for (int i=0;i<10000;i++){counter--;}}
}
  • 修饰代码块:synchronized 修饰代码块,则其所用的锁,是某个指定 Java 对象
public class Test04 {public static void main(String[] args) {Thread add = new Thread(new AddThread());Thread dec = new Thread(new DecThread());add.start();dec.start();try {add.join();dec.join();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Count.count);}
}class Count{public static int count = 0; //公共的累加值public static Object lock = new Object(); // 锁对象
}// 累加线程
class AddThread implements Runnable{@Overridepublic void run() {for (int i = 0; i<10000;i++){// synchronized 同步锁synchronized (Count.lock) {// 加锁// 临界区Count.count += 1;}// 释放锁}}
}// 累减线程
class DecThread implements Runnable{@Overridepublic void run() {for (int i = 0; i<10000;i++){// synchronized 同步锁synchronized (Count.lock) {// 加锁// 临界区Count.count -= 1;}// 释放锁}}
}

http://www.xdnf.cn/news/15455.html

相关文章:

  • UE5多人MOBA+GAS 23、制作一个地面轰炸的技能
  • SHAP 值的数值尺度
  • 梳理Bean的创建流程
  • burpsuite使用中遇到的一些问题(bp启动后浏览器无法连接)/如何导入证书
  • GPIO 输入/输出
  • 2025年睿抗机器人开发者大赛CAIP-编程技能赛-高职组(省赛)解题报告 | 珂学家
  • 在Autodl服务器中使用VNC建立图形界面
  • 快速排序:原理、示例与 C 语言实现详解
  • C语言---自定义类型(下)(枚举和联合类型)
  • 云服务器如何管理数据库(MySQL/MongoDB)?
  • 【html常见页面布局】
  • 50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | DoubleClickHeart(双击爱心)
  • netstat -tlnp | grep 5000
  • Swift实现股票图:从基础到高级
  • python的形成性考核管理系统
  • 并发-原子变量类
  • 【MCU控制 初级手札】1.1 电阻
  • 现代CSS实战:用变量与嵌套重构可维护的前端样式
  • 使用 Java 获取 PDF 页面信息(页数、尺寸、旋转角度、方向、标签与边框)
  • Flink双流实时对账
  • 大语言模型零样本情感分析实战:无需机器学习训练,96%准确率实现指南
  • 云手机隐私保护指南:如何保障账号与数据的云端安全?
  • 虚拟机删除操作
  • IELTS 阅读C15-test1-passage 2 复盘
  • React源码6 三大核心模块之一:commit, finishConcurrentRender函数
  • 24.找到列表中最大或最小值的索引
  • Pitaya 是一个简单、快速、轻量级的游戏服务器框架,它为分布式多人游戏和服务器端应用程序提供了一个基本的开发框架
  • 优雅的Java:01.数据更新如何更优雅
  • Python学习之路(十二)-开发和优化处理大数据量接口
  • 从springcloud-gateway了解同步和异步,webflux webMvc、共享变量