Java之线程详解(二)——线程安全概述、synchronized锁
创始人
2024-03-09 08:05:08

一、线程安全概述

  1. 什么是线程安全问题?

当多个线程共享同一个全局变量,做写的操作时(即修改该全局变量),可能会受到其他的线程干扰,发生线程安全问题。
eg:

public class Thread01  implements  Runnable{//定义一个全局变量private static Integer count = 100;@Overridepublic void run() {while (count >1){cal();}}private void cal(){try {Thread.sleep(30);} catch (InterruptedException e) {e.printStackTrace();}count--;System.out.println(Thread.currentThread().getName()+"--"+count);}public static void main(String[] args) {Thread01 thread01 = new Thread01();Thread thread1 = new Thread(thread01);Thread thread2 = new Thread(thread01);thread1.start();thread2.start();}
}

运行后打印结果出现了线程安全问题,如下图:
在这里插入图片描述

  1. 多线程如何解决线程安全问题(多线程如何实现同步)

当多个线程共享同一个全局变量时,将可能会发生线程安全的代码上锁,保证只有拿到锁的线程才可以执行,没有拿到锁的线程不可以执行,需要阻塞等待。
(1)使用synchronized锁;
(2)使用Lock锁 ,需要自己实现锁的升级过程,底层是基于aqs实现;
(3)使用Threadlocal,需要注意内存泄漏的问题。
(4)原子类CAS 非阻塞式。

二、synchronized锁的基本用法

  1. 修饰代码块
    修饰代码块,指定加锁对象,对指定对象加锁,执行该代码块前要获得指定对象的锁。
    eg:
public class Thread01 implements Runnable{private static Integer count = 100;@Overridepublic void run() {while (count > 1) {test();}}private void test() {synchronized (this) {try {Thread.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}count--;System.out.println(Thread.currentThread().getName() + "," + count);}}public static void main(String[] args) {Thread01 thread01 = new Thread01();Thread thread1 = new Thread(thread01);Thread thread2 = new Thread(thread01);thread1.start();thread2.start();}
}
  1. 修饰实例方法
    修饰实例方法,作用于当前实例加锁,进入同步代码前要获得当前实例的锁,在实例方法上默认加上synchronized 默认使用this锁。
    eg:
public class Thread02 implements Runnable {private static Integer count = 100;@Overridepublic void run() {while (count > 1) {test();}}private synchronized void test() {try {Thread.sleep(10);} catch (Exception e) {}count--;System.out.println(Thread.currentThread().getName() + "," + count);}public static void main(String[] args) {Thread02 thread02 = new Thread02();Thread thread1 = new Thread(thread02);Thread thread2 = new Thread(thread02);thread1.start();thread2.start();}
}
  1. 修饰静态方法
    修饰静态方法,作用于当前类对象加锁,进入同步代码前要获得当前类对象的锁,默认使用当前类的 类名.class 锁。
    eg:
public class Thread03 implements Runnable{private static Integer count = 100;private static String lock = "lock";@Overridepublic void run() {while (count > 1) {test();}}private static void test() {synchronized (Thread03.class) {try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}count--;System.out.println(Thread.currentThread().getName() + "," + count);}}public static void main(String[] args) {Thread03 Thread031 = new Thread03();Thread03 Thread032 = new Thread03();Thread thread1 = new Thread(Thread031);Thread thread2 = new Thread(Thread032);thread1.start();thread2.start();}
}
  1. synchronized死锁问题
    在使用synchronized时需要注意 synchronized锁嵌套的问题,避免死锁的问题发生。
    eg:
public class ThreadDeadlock implements Runnable{private int count = 1;private String lock = "lock";@Overridepublic void run() {while (true) {count++;if (count % 2 == 0) {// 线程1需要先获取到自定义对象的lock锁,执行test1方法需要再获取this锁;线程2需要先获取this锁,执行test2方法再获取lock锁synchronized (lock) {test1();}} else {synchronized (this) {test2();}}}}public synchronized void test1() {System.out.println(Thread.currentThread().getName() + ",test1方法");}public void test2() {synchronized (lock) {System.out.println(Thread.currentThread().getName() + ",test2方法");}}public static void main(String[] args) {ThreadDeadlock threadDeadlock = new ThreadDeadlock();Thread thread1 = new Thread(threadDeadlock);Thread thread2 = new Thread(threadDeadlock);thread1.start();thread2.start();}
}
  1. springmvc 接口中使用
    Spring MVC 的Controller默认是单例的,需要注意线程安全问题。
    eg:
@RestController
public class ThreadService {private int count = 0;@RequestMapping("/test")public synchronized String count() {try {System.out.println(">count<" + count++);Thread.sleep(3000);} catch (Exception e) {e.printStackTrace();}return "count";}
}
  1. 多线程线程之间通讯

等待/通知机制:
等待/通知的相关方法是任意Java对象都具备的,因为这些方法被定义在所有对象的超类java.lang.Object上,方法如下:
(1)notify():通知一个在对象上等待的线程,使其从main()方法返回,而返回的前提是该线程获取到了对象的锁;
(2)notifyAll():通知所有等待在该对象的线程;
(3)wait():调用该方法的线程进入WAITING状态,只有等待其他线程的通知或者被中断,才会返回。需要注意调用wait()方法后,会释放对象的锁 。

注意:wait()、notify()和notifyAll()方法要与synchronized一起使用。

eg:

public class Thread02 {private  Object objectLock = new Object();public static void main(String[] args) throws InterruptedException {new Thread02().print();}public void print() throws InterruptedException {new Thread(new Runnable() {@Overridepublic void run() {synchronized (objectLock){System.out.println(Thread.currentThread().getName()+"---1---");try {objectLock.wait();} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName()+"---2---");}}}).start();Thread.sleep(3000);synchronized (objectLock){objectLock.notify();}}
}
  1. 多线程通讯实现生产者与消费者
    eg:
public class Thread03 {class Student {public String name;public char sex;public boolean flag = true;//true:输入,false:输出}class InputThread extends Thread {private Student student;public InputThread(Student student) {this.student = student;}@Overridepublic void run() {int count = 0;while (true) {synchronized (student) {if (!student.flag) {try {student.wait();} catch (InterruptedException e) {e.printStackTrace();}}if (count == 0) {student.name = "毛毛";student.sex = '男';} else {student.name = "天天";student.sex = '女';}count = (count + 1) % 2;student.flag = false;student.notify();}}}}class PrintThread extends Thread {private Student student;public PrintThread(Student student) {this.student = student;}@Overridepublic void run() {while (true) {synchronized (student) {if (student.flag) {try {student.wait();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("姓名:" + student.name + ",性别:" + student.sex);student.flag = true;student.notify();}}}}public void print() {Student student = new Student();InputThread inputThread = new InputThread(student);PrintThread printThread = new PrintThread(student);inputThread.start();printThread.start();}public static void main(String[] args) {new Thread03().print();}}

相关内容

热门资讯

埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...