当前位置:网站首页>线程API
线程API
2022-08-09 07:08:00 【lonesomee】
sleep阻塞
线程提供了一个静态方法:
- static void sleep(long ms)
- 使运行该方法的线程进入阻塞状态指定的毫秒,超时后线程会自动回到RUNNABLE状态等待再次获取时间片并发运行.
public class SleepDemo {
public static void main(String[] args) {
System.out.println("程序开始了!");
try {
Thread.sleep(5000);//主线程阻塞5秒钟
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("程序结束了!");
}
}
sleep方法处理异常:InterruptedException.
当一个线程调用sleep方法处于睡眠阻塞的过程中,该线程的interrupt()方法被调用时,sleep方法会抛出该异常从而打断睡眠阻塞.
守护线程与普通线程的区别:守护线程是通过普通线程调用setDaemon(true)设置而来的
主要区别体现在当java进程中所有的普通线程都结束时进程会结束,在结束前会杀死所有还在运行的守护线程。
public class DaemonThreadDemo {
public static void main(String[] args) {
Thread demo1 = new Thread(){
public void run(){
for(int i=0;i<5;i++){
System.out.println("222");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
System.out.println("111");
}
};
Thread demo2 = new Thread(){
public void run(){
while(true){
System.out.println("333");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
};
demo1.start();
demo2.setDaemon(true);//设置守护线程必须在线程启动前进行
demo2.start();
}
}
重点:多线程并发安全问题
- 什么是多线程并发安全问题:
当多个线程并发操作同一临界资源,由于线程切换时机不确定,导致执行顺序出现混乱。
解决办法:
将并发操作改为同步操作就可有效的解决多线程并发安全问题 - 同步与异步的概念:同步和异步都是说的多线程的执行方式。
多线程各自执行各自的就是异步执行,而多线程执行出现了先后顺序进行就是同步执行 public class SyncDemo { public static void main(String[] args) { Table table = new Table(); Thread t1 = new Thread(){ public void run(){ while(true){ int bean = table.getBean(); Thread.yield(); System.out.println(getName()+":"+bean); } } }; Thread t2 = new Thread(){ public void run(){ while(true){ int bean = table.getBean(); /* static void yield() 线程提供的这个静态方法作用是让执行该方法的线程 主动放弃本次时间片。 这里使用它的目的是模拟执行到这里CPU没有时间了,发生 线程切换,来看并发安全问题的产生。 */ Thread.yield(); System.out.println(getName()+":"+bean); } } }; t1.start(); t2.start(); } } class Table{ private int beans = 20;//桌子上有20个豆子 public int getBean(){ if(beans==0){ throw new RuntimeException("没有豆子了!"); } Thread.yield(); return beans--; } }
synchronized的两种用法
- 1.直接在方法上声明,此时该方法称为同步方法,同步方法同时只能被一个线程执行
2.同步块,推荐使用。同步块可以更准确的控制需要同步执行的代码片段。
有效的缩小同步范围可以在保证并发安全的前提下提高并发效率 public class SyncDemo { public static void main(String[] args) { Table table = new Table(); Thread t1 = new Thread(){ public void run(){ while(true){ int bean = table.getBean(); Thread.yield(); System.out.println(getName()+":"+bean); } } }; Thread t2 = new Thread(){ public void run(){ while(true){ int bean = table.getBean(); /* static void yield() 线程提供的这个静态方法作用是让执行该方法的线程 主动放弃本次时间片。 这里使用它的目的是模拟执行到这里CPU没有时间了,发生 线程切换,来看并发安全问题的产生。 */ Thread.yield(); System.out.println(getName()+":"+bean); } } }; t1.start(); t2.start(); } } class Table{ private int beans = 20;//桌子上有20个豆子 /** * 当一个方法使用synchronized修饰后,这个方法称为同步方法,多个线程不能 * 同时执行该方法。 * 将多个线程并发操作临界资源的过程改为同步操作就可以有效的解决多线程并发 * 安全问题。 * 相当于让多个线程从原来的抢着操作改为排队操作。 */ public synchronized int getBean(){ if(beans==0){ throw new RuntimeException("没有豆子了!"); } Thread.yield(); return beans--; } }
- 同步监视器对象的选取:
对于同步的成员方法而言,同步监视器对象不可指定,只能是this
对于同步的静态方法而言,同步监视器对象也不可指定,只能是类对象
对于同步块而言,需要自行指定同步监视器对象,选取原则:
1.必须是引用类型
2.多个需要同步执行该同步块的线程看到的该对象必须是同一个 - 互斥性
当使用多个synchronized修饰了多个代码片段,并且指定的同步监视器都是同一个对象时,这些代码片段就是互斥的,多个线程不能同时在这些代码片段上执行。
public class SyncDemo4 {
public static void main(String[] args) {
Foo foo = new Foo();
Thread t1 = new Thread(){
public void run(){
foo.methodA();
}
};
Thread t2 = new Thread(){
public void run(){
foo.methodB();
}
};
t1.start();
t2.start();
}
}
class Foo{
public synchronized void methodA(){
Thread t = Thread.currentThread();
try {
System.out.println(t.getName()+":正在执行A方法...");
Thread.sleep(5000);
System.out.println(t.getName()+":执行A方法完毕!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void methodB(){
Thread t = Thread.currentThread();
try {
System.out.println(t.getName()+":正在执行B方法...");
Thread.sleep(5000);
System.out.println(t.getName()+":执行B方法完毕!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
边栏推荐
- Fragments
- 排序第一节——插入排序(直接插入排序+希尔排序)(视频讲解26分钟)
- Classes and Structures
- jvm线程状态
- Altium designer software commonly used the most complete package library, including schematic library, PCB library and 3D model library
- 力扣 636. 函数的独占时间
- 975. 奇偶跳 有序集合
- 数据一致性架构
- Lottie系列一:介绍与使用
- The division principle summary within the collection
猜你喜欢
用tensorflow.keras模块化搭建神经网络模型
分布式理论
redis学习笔记
Leetcode 70 stairs issues (Fibonacci number)
更改Jupyter Notebook默认打开目录
Important news丨.NET Core 3.1 will end support on December 13 this year
postgresql Window Functions
(本章节完结)排序第五节——非比较排序(计数排序+基数排序+桶排序)(附有自己的视频讲解)
【Template】Tree Chain Segmentation P3384
无重复的字符的最长子串
随机推荐
stm32定时器之简单封装
【Reprint】Deep Learning (deep learning) study notes arrangement
高项 03 项目立项管理
RK3568商显版开源鸿蒙板卡产品解决方案
买口罩(0-1背包)
MUI无法滚动?完美解决
Codeforces Round #359 (Div. 2) C. Robbers' watch Violent Enumeration
查看日志常用命令
Better Scroll Y上下滚动无法上拉滚动解决办法
类和结构体
多米诺骨牌
【MySQL】update mysql.user set authentication_string=password(“123456“) where User=‘root‘; 报错
Unity first lesson
高德地图JS - 已知经纬度来获取街道、城市、详细地址等信息
(本章节完结)排序第五节——非比较排序(计数排序+基数排序+桶排序)(附有自己的视频讲解)
重要消息丨.NET Core 3.1 将于今年12月13日结束支持
Leetcode 70 stairs issues (Fibonacci number)
分布式事务的应用场景
常见的分布式事务解决方案
95后,刚工作2-3年就年薪50W+ ,才发现打败我们的,从来不是年龄···