当前位置:网站首页>线程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();
}
}
}
边栏推荐
猜你喜欢
Altium designer software commonly used the most complete package library, including schematic library, PCB library and 3D model library
当酷雷曼VR直播遇上视频号,会摩擦出怎样的火花?
【Oracle 11g】Redhat 6.5 安装 Oracle11g
子路由及路由出口配置
找不到和chrome浏览器版本不同的chromedriver的解决方法
Zero shift of leetcode
【报错】Root Cause com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The JVM thread state
Colors that Tkinter can choose from
jmeter并发数量以及压力机的一些限制
随机推荐
Lottie系列四:使用建议
错误:为 repo ‘oracle_linux_repo‘ 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.
jmeter concurrency and some limitations of the press
Difference Constraint - Graph Theory
mysql summary
bzoj 5333 [Sdoi2018]荣誉称号
DSP+ARM+FPGA高速PCIE/千兆网口信号仿真介绍
HDU - 3183 A Magic Lamp 线段树
无重复的字符的最长子串
入门cv必读的10篇baseline论文
图论,二叉树,dfs,bfs,dp,最短路专题
【Reprint】Deep Learning (deep learning) study notes arrangement
【Docker】Docker安装MySQL
虚拟机网卡报错:Bringing up interface eth0: Error: No suitable device found: no device found for connection
View log common commands
分布式id 生成器实现
Neural Network Optimizer
Rsync常见错误
Example of using the cut command
P6 ali machine test of 2020 Fibonacci number