更多内容,点击了解: https://how2j.cn/k/thread/thread-synchronized/355.html
步骤1:演示同步问题
步骤2:分析同步问题产生的原因
步骤3:解决思路
步骤4:synchronized 同步对象概念
步骤5:使用synchronized 解决同步问题
步骤6:使用hero对象作为同步对象
步骤7:在方法前,加上修饰符synchronized
步骤8:线程安全的类
步骤 1 : 演示同步问题
假设盖伦有10000滴血,并且在基地里,同时又被对方多个英雄***
就是有多个线程在减少盖伦的hp
同时又有多个线程在恢复盖伦的hp
假设线程的数量是一样的,并且每次改变的值都是1,那么所有线程结束后,盖伦应该还是10000滴血。
但是。。。
注意: 不是每一次运行都会看到错误的数据产生,多运行几次,或者增加运行的次数
package charactor; public class Hero{public String name;public float hp; public int damage; //回血public void recover(){hp=hp+1;} //掉血public void hurt(){hp=hp-1;} public void attackHero(Hero h) {h.hp-=damage;System.out.format("%s 正在*** %s, %s的血变成了 %.0f%n",name,h.name,h.name,h.hp);if(h.isDead())System.out.println(h.name +"死了!");} public boolean isDead() {return 0>=hp?true:false;} }package multiplethread;import charactor.Hero;public class TestThread {public static void main(String[] args) {final Hero gareen = new Hero();gareen.name = "盖伦";gareen.hp = 10000; System.out.printf("盖伦的初始血量是 %.0f%n", gareen.hp); //多线程同步问题指的是多个线程同时修改一个数据的时候,导致的问题 //假设盖伦有10000滴血,并且在基地里,同时又被对方多个英雄*** //用JAVA代码来表示,就是有多个线程在减少盖伦的hp//同时又有多个线程在恢复盖伦的hp //n个线程增加盖伦的hp int n = 10000; Thread[] addThreads = new Thread[n];Thread[] reduceThreads = new Thread[n]; for (int i = 0; i < n; i++) {Thread t = new Thread(){public void run(){gareen.recover();try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};t.start();addThreads[i] = t; } //n个线程减少盖伦的hpfor (int i = 0; i < n; i++) {Thread t = new Thread(){public void run(){gareen.hurt();try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};t.start();reduceThreads[i] = t;} //等待所有增加线程结束for (Thread t : addThreads) {try {t.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//等待所有减少线程结束for (Thread t : reduceThreads) {try {t.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}} //代码执行到这里,所有增加和减少线程都结束了 //增加和减少线程的数量是一样的,每次都增加,减少1.//那么所有线程都结束后,盖伦的hp应该还是初始值 //但是事实上观察到的是: System.out.printf("%d个增加线程和%d个减少线程结束后%n盖伦的血量变成了 %.0f%n", n,n,gareen.hp); }}
步骤 2 : 分析同步问题产生的原因
步骤 3 : 解决思路
总体解决思路是: 在增加线程访问hp期间,其他线程不可以访问hp
步骤 4 : synchronized 同步对象概念
解决上述问题之前,先理解
synchronized关键字的意义
如下代码:
Object someObject =new Object();synchronized (someObject){ //此处的代码只有占有了someObject后才可以执行}
synchronized表示当前线程,独占 对象 someObject
当前线程独占 了对象someObject,如果有其他线程试图占有对象someObject,就会等待,直到当前线程释放对someObject的占用。
someObject 又叫同步对象,所有的对象,都可以作为同步对象
为了达到同步的效果,必须使用同一个同步对象
释放同步对象的方式: synchronized 块自然结束,或者有异常抛出
package multiplethread; import java.text.SimpleDateFormat;import java.util.Date; public class TestThread { public static String now(){return new SimpleDateFormat("HH:mm:ss").format(new Date());} public static void main(String[] args) {final Object someObject = new Object(); Thread t1 = new Thread(){public void run(){try {System.out.println( now()+" t1 线程已经运行");System.out.println( now()+this.getName()+ " 试图占有对象:someObject");synchronized (someObject) { System.out.println( now()+this.getName()+ " 占有对象:someObject");Thread.sleep(5000);System.out.println( now()+this.getName()+ " 释放对象:someObject");}System.out.println(now()+" t1 线程结束");} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};t1.setName(" t1");t1.start();Thread t2 = new Thread(){ public void run(){try {System.out.println( now()+" t2 线程已经运行");System.out.println( now()+this.getName()+ " 试图占有对象:someObject");synchronized (someObject) {System.out.println( now()+this.getName()+ " 占有对象:someObject");Thread.sleep(5000);System.out.println( now()+this.getName()+ " 释放对象:someObject");}System.out.println(now()+" t2 线程结束");} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};t2.setName(" t2");t2.start();} }
步骤 5 : 使用synchronized 解决同步问题
所有需要修改hp的地方,有要建立在占有someObject的基础上。
而对象 someObject在同一时间,只能被一个线程占有。 间接地,导致同一时间,hp只能被一个线程修改。
package multiplethread; import java.awt.GradientPaint; import charactor.Hero; public class TestThread { public static void main(String[] args) { final Object someObject = new Object(); final Hero gareen = new Hero();gareen.name = "盖伦";gareen.hp = 10000; int n = 10000; Thread[] addThreads = new Thread[n];Thread[] reduceThreads = new Thread[n]; for (int i = 0; i < n; i++) {Thread t = new Thread(){public void run(){ //任何线程要修改hp的值,必须先占用someObjectsynchronized (someObject) {gareen.recover();} try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};t.start();addThreads[i] = t; } for (int i = 0; i < n; i++) {Thread t = new Thread(){public void run(){//任何线程要修改hp的值,必须先占用someObjectsynchronized (someObject) {gareen.hurt();} try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};t.start();reduceThreads[i] = t;} for (Thread t : addThreads) {try {t.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}for (Thread t : reduceThreads) {try {t.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}} System.out.printf("%d个增加线程和%d个减少线程结束后%n盖伦的血量是 %.0f%n", n,n,gareen.hp); } }
步骤 6 : 使用hero对象作为同步对象
既然任意对象都可以用来作为同步对象,而所有的线程访问的都是同一个hero对象,索性就使用gareen来作为同步对象
进一步的,对于Hero的hurt方法,加上:
synchronized (this) {
}
表示当前对象为同步对象,即也是gareen为同步对象
TestThread.java
Hero.java
package multiplethread; import java.awt.GradientPaint; import charactor.Hero; public class TestThread { public static void main(String[] args) { final Hero gareen = new Hero();gareen.name = "盖伦";gareen.hp = 10000; int n = 10000; Thread[] addThreads = new Thread[n];Thread[] reduceThreads = new Thread[n]; for (int i = 0; i < n; i++) {Thread t = new Thread(){public void run(){ //使用gareen作为synchronizedsynchronized (gareen) {gareen.recover();} try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};t.start();addThreads[i] = t; } for (int i = 0; i < n; i++) {Thread t = new Thread(){public void run(){//使用gareen作为synchronized//在方法hurt中有synchronized(this)gareen.hurt(); try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};t.start();reduceThreads[i] = t;} for (Thread t : addThreads) {try {t.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}for (Thread t : reduceThreads) {try {t.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}} System.out.printf("%d个增加线程和%d个减少线程结束后%n盖伦的血量是 %.0f%n", n,n,gareen.hp); } }
package charactor; public class Hero{public String name;public float hp; public int damage; //回血public void recover(){hp=hp+1;} //掉血public void hurt(){//使用this作为同步对象synchronized (this) {hp=hp-1; }} public void attackHero(Hero h) {h.hp-=damage;System.out.format("%s 正在*** %s, %s的血变成了 %.0f%n",name,h.name,h.name,h.hp);if(h.isDead())System.out.println(h.name +"死了!");} public boolean isDead() {return 0>=hp?true:false;} }
步骤 7 : 在方法前,加上修饰符synchronized
在recover前,直接加上synchronized ,其所对应的同步对象,就是this
和hurt方法达到的效果是一样
外部线程访问gareen的方法,就不需要额外使用synchronized 了
Hero.java
TestThread.java
package charactor; public class Hero{public String name;public float hp; public int damage; //回血//直接在方法前加上修饰符synchronized//其所对应的同步对象,就是this//和hurt方法达到的效果一样public synchronized void recover(){hp=hp+1;} //掉血public void hurt(){//使用this作为同步对象synchronized (this) {hp=hp-1; }} public void attackHero(Hero h) {h.hp-=damage;System.out.format("%s 正在*** %s, %s的血变成了 %.0f%n",name,h.name,h.name,h.hp);if(h.isDead())System.out.println(h.name +"死了!");} public boolean isDead() {return 0>=hp?true:false;} }package multiplethread; import java.awt.GradientPaint; import charactor.Hero; public class TestThread { public static void main(String[] args) { final Hero gareen = new Hero();gareen.name = "盖伦";gareen.hp = 10000; int n = 10000; Thread[] addThreads = new Thread[n];Thread[] reduceThreads = new Thread[n]; for (int i = 0; i < n; i++) {Thread t = new Thread(){public void run(){ //recover自带synchronizedgareen.recover(); try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};t.start();addThreads[i] = t; } for (int i = 0; i < n; i++) {Thread t = new Thread(){public void run(){//hurt自带synchronizedgareen.hurt(); try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};t.start();reduceThreads[i] = t;} for (Thread t : addThreads) {try {t.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}for (Thread t : reduceThreads) {try {t.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}} System.out.printf("%d个增加线程和%d个减少线程结束后%n盖伦的血量是 %.0f%n", n,n,gareen.hp); } }
步骤 8 : 线程安全的类
如果一个类,其方法都是有synchronized修饰的,那么该类就叫做线程安全的类
同一时间,只有一个线程能够进入 这种类的一个实例 的去修改数据,进而保证了这个实例中的数据的安全(不会同时被多线程修改而变成脏数据)
比如StringBuffer和StringBuilder的区别
StringBuffer的方法都是有synchronized修饰的,StringBuffer就叫做线程安全的类
而StringBuilder就不是线程安全的类
更多内容,点击了解: https://how2j.cn/k/thread/thread-synchronized/355.html