2022-08-03 第二小组 张鑫 学习笔记
实训二十六天 多线程
1.学习重点
1.锁
2.线程重入
3.方法总结
2.学习心得
人麻了,理论知识真是不好理解啊,有些时候看代码真的看不懂为什么可以直接调用或者写在括号里,还是以前的知识学的不够扎实,找时间一定好好复习
3.学习内容
锁
synchronized多线程并发编程
重量级锁。jdk1.6对synchronized进行了优化
jdk1.6为了减少获得锁和释放锁带来的性能消耗引入了偏向锁和轻量锁
synchronized三种方式加锁。分别是:
- 修饰实例方法,作用于当前实例加锁,进入同步代码之前要获得当前实例的锁
- 静态方法,作用于当前类对象加锁,进入同步代码之前要获得当前类对象的锁
- 代码块,指定加锁对象,对给定对象加锁,进入同步代码块之前要获得给定对象的锁
实例方法:调用该方法的实例
静态方法:类对象
this:调用该方法的实例对象
类对象:类对象
操作共享数据的代码
共享数据,多个线程共同操作的变量,都可以充当锁
public class Ch01 {
public static void main(String[] args) {
//同步代码块
//创建一个对象
//类对象
//当前实例this
//同步监视器
synchronized (Ch01.class){
int a=1;
}
}
}
当使用同步方法时,synchronized锁的东西是this(默认的)
关于同步方法
1.同步方法依然涉及到同步锁对象,不需要我们写出来
2.非静态的同步方法,同步锁就是this
静态的同步方法,同步监视器就是类本身
同步代码块
1.选好同步监视器(锁)推荐使用类对象,第三方对象,this
2.在实现接口创建的线程类中,同步代码块不可以用this来充当同步锁
同步的方式,解决线程安全的问题。
操作同步代码时,只有一个线程能够参与,其他线程等待。
相当于一个单线程的过程,效率低。
synchronized只针对于当前JVM可以解决线程安全问题。
synchronized不可以跨JVM解决问题!!!
死锁
死锁是这样一种情形:多个线程同时被阻塞,他们中的一个或者全部
都在等待某个资源的释放,由于线程无限期的阻塞,程序就不可能正常终止。
java死锁产生四个必要条件
- 互斥使用,当资源被一个线程使用(占用),别的线程不能使用。
- 不可抢占,资源请求者不能强制从占有者中抢夺资源,资源只能从占有者手动释放
- 请求和保持,
- 循环等待,存在一个等待队列。P1占有P2的资源,P2占有了P3的资源,P3占有P1的资源。形成了一个等待环路。
线程重入
任意线程在拿到锁之后,再次获取该锁不会被该锁所阻碍
线程不会被自己锁死的。
这就叫线程的重入,synchronized可重入锁。
JDK1.6以后锁升级:
1.无锁:不加锁
2.偏向锁:不锁锁,只有一个线程争夺时,偏向某一个线程,这个线程不加锁
3.轻量级锁:少量线程来了之后,向尝试自旋,不挂起线程。
4.重量级锁:排队挂起(暂停)线程。(synchronized)
挂起线程和恢复线程需要转入内核态中完成这些操作,给系统的并发性带来很大的压力。
在许多应用上共享数据的锁定状态,只会持续很短的时间,为了这段时间去挂起和恢复并不值得
我们可以让后面的线程等待一下,不要放弃处理器的执行时间。锁为了让线程等待,我们只需要让线程
执行一个循环,自旋。【自旋锁】
hashcode值1 0
public class Ch05 {
private static final Object M1 = new Object();
private static final Object M2 = new Object();
public static void main(String[] args) {
new Thread(() -> {
synchronized (M1) {
synchronized (M2) {
synchronized (M1) {
synchronized (M2){
System.out.println("hello lock");
}
}
}
}
}).start();
}
}
Object类对多线程的支持
- wait():
- wait(long timeout):当前线程进入等待状态
- notify():唤醒正在等待的下一个线程
- notifyAll():唤醒正在等待的所有线程
线程间的通信
比如两条线程,共同运行。
线程A如果先走,线程B就要等待。等待线程A走完,唤醒线程B,线程B再走
public class Ch01 {
private static int num = 10;
private static final Object OBJ = new Object();
public static void plus(int code,int i) {
synchronized (OBJ) {
if(num >= 10){
// 唤醒其他等待的线程
// OBJ.notify();
// 唤醒所有等待的线程
OBJ.notifyAll();
}
System.out.println("这是线程" + code + "->" + ++num + "->" + i);
}
}
public static void sub(int code,int i) {
synchronized (OBJ) {
if(num <= 0){
try {
// 减法上来就是等待的状态
OBJ.wait(200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("这是线程" + code + "->" + --num + "->" + i);
}
}
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
for (int i = 0;; i++) {
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
sub(1,i);
}
});
Thread t2 = new Thread(() -> {
for (int i = 0;; i++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
plus(2,i);
}
});
t1.start();
t2.start();
System.out.println("--------------------------------------------------");
}
}
方法总结
1.Thread的两个静态方法
- sleep释放CPU资源,但是不会释放锁
- yield方法释放CPU执行权,保留了CPU的执行资格,不常用。
2.join方法,yield出让了执行权,join就加入进来。
3.wait:释放CPU资源,释放锁
- notify:唤醒等待中的线程
- notifyAll:唤醒等待中的所有线程
案例
生产者与消费者模型
两条线程,一条线程生产产品,另一条线程消费产品
思路:
这两条线程,初始状态是什么情况?
电脑工厂,生产电脑是需要时间。生产完毕100台电脑。
唤醒这些等待的消费者。等待。唤醒工厂,继续生产
消费者,等待,被唤醒,100台电脑都卖出去了,等待
class Factory implements Runnable {
@Override
public void run() {
synchronized (Ch02.OBJ){
while(true){
// 生产电脑
System.out.println("工厂生产电脑,已经生产了:" + Ch02.count ++ + "台!");
if(Ch02.count >= 100) {
notifyAll();
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
class Consumer implements Runnable {
@Override
public void run() {
synchronized (Ch02.OBJ){
while(true){
if(Ch02.count >= 0){
// 消费电脑
System.out.println("消费者消费了1台电脑,剩余:" + Ch02.count-- + "台!");
}
if(Ch02.count <= 0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
notifyAll();
}
}
}
}
}
public class Ch02 {
public static final Object OBJ = new Object();
public static int count = 0;
public static void main(String[] args) {
Thread t1 = new Thread(new Factory());
Thread t2 = new Thread(new Consumer());
t1.start();
t2.start();
}
}
线程的退出
使用退出标志,线程正常退出,run方法结束后线程终止
不要使用stop方法。
System.exit(-1)
class MyThread extends Thread {
volatile boolean flag = true;
@Override
public void run() {
while(flag) {
try {
System.out.println("线程一直在运行...");
int i = 10 / 0;
} catch (Exception e) {
this.stopThread();
}
}
}
public void stopThread() {
System.out.println("线程停止运行...");
this.flag = false;
}
}
public class Ch03 {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
interrupt方法
中断线程
调用interrupt方法会抛出InterruptedException异常,
捕获后再做停止线程的逻辑即可。
如果线程while(true)运行的状态,interrupt方法无法中断线程。
class MyThread02 extends Thread {
private boolean flag = true;
@Override
public void run() {
while(flag) {
synchronized (this){
// try {
// wait();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
try {
sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
this.stopThread();
}
}
}
}
public void stopThread() {
System.out.println("线程停止运行...");
this.flag = false;
}
}
public class Ch04 {
// public void show() {
// try {
// wait();
// // 线程异常终止 异常
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
public static void main(String[] args) {
MyThread02 myThread02 = new MyThread02();
myThread02.start();
System.out.println("线程开始...");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 中断线程的执行
myThread02.interrupt();
}
}
线程的常用方法
Thread类中的方法
1.start:启动当前线程;执行run方法
2.run:
3.currentThread:静态方法,获取当前正在执行的线程
4.getId():返回此线程的唯一标识
5.setName(String):设置当前线程的name
6.getName():获取当前线程的name
7.getPriority():获取当前线程的优先级
8.setPriority(int):设置当前线程的优先级
9.getState():获取当前线程的声明周期
10.interrupt():中断线程的执行
11.interrupted():查看当前线程是否中断
案例
CarConsumer类
public class CarConsumer implements Runnable {
private Car car;
public CarConsumer() {
}
public CarConsumer(Car car) {
this.car = car;
}
@Override
public synchronized void run() {
for (int i = 1; i <= 100; i++) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
car.get();
}
}
}
CarProduction
public class CarProduction implements Runnable {
private Car car;
public CarProduction() {
}
public CarProduction(Car car) {
this.car = car;
}
@Override
public synchronized void run() {
for (int i = 1; i <= 100 ; i++) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(i % 2 == 0){
car.set("汽车" + i,i);
}else {
car.set("卡车" + i,i);
}
}
}
}
Car
public class Car {
private String name;
private Integer id;
private boolean flag = false;
public Car() {
}
public Car(String name, Integer id) {
this.name = name;
this.id = id;
}
// 买车
public synchronized void get() {
if(!flag) {
// 要等待汽车的生产
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("购买了汽车" + this.name + "->" + this.id);
flag = false;
super.notify();
}
// 产车
public synchronized void set(String name,Integer id){
if(flag) {
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.id = id;
System.out.println("生产了汽车:" + name + "->" + id);
flag = true;
super.notify();
}
}
实现类
public class Demo {
public static void main(String[] args) {
Car car = new Car();
CarProduction carProduction = new CarProduction(car);
CarConsumer carConsumer = new CarConsumer(car);
new Thread(carConsumer).start();
new Thread(carProduction).start();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效