Java多线程安全问题解决的两种方式代码案例
生活随笔
收集整理的這篇文章主要介紹了
Java多线程安全问题解决的两种方式代码案例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
需求:用三個線程模擬三個售票窗口,共同賣100張火車票,每個線程打印出賣第幾張票.
多線程安全問題
需求:用三個線程模擬三個售票窗口,共同賣100張火車票,每個線程打印出賣第幾張票.
模擬安全問題
public class TicketThread implements Runnable {int tickets = 100;//火車票數(shù)量@Overridepublic void run() {//出售火車票while(true) {//當火車票小于0張,則停止售票if(tickets > 0) {/** t1,t2,t3* 假設只剩一張票* t1過來了,他一看有票,他就進來了,但是他突然肚子不舒服,然后他就去上衛(wèi)生間了* t2也過來了,他一看也有票,他也進來了,但是他的肚子也不舒服,他也去上衛(wèi)生間了* * t1上完了衛(wèi)生間回來了,開始售票* tickets = 0;* t2也上完衛(wèi)生間回來了,他也進行售票* tickets = -1;* * */try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(Thread.currentThread().getName() + ":" +tickets--);}}}}使用同步代碼塊解決多線程安全問題
格式
格式:
synchronized(鎖對象){
//需要同步的代碼
}
案例代碼
/** 問題出現(xiàn)的原因:* 要有多個線程* 要有被多個線程所共享的數(shù)據(jù)* 多個線程并發(fā)的訪問共享的數(shù)據(jù)* * 在火車上上廁所* 張三來了,一看門是綠的,他就進去了,把門鎖上了,門就變紅了* 李四來了,一看門市紅色的,他就只能憋著* 張三用完了廁所,把鎖打開了,門就變成了綠色* 李四一看門變綠了,他就進去了,把門鎖上,門就變紅了* 王五來了,一看們是紅色的,他也只能憋著* 李四用完測試了,把鎖打開了,肚子又不舒服了,扭頭回去了,又把門鎖上了,* * synchronized:同步(鎖),可以修飾代碼塊和方法,被修飾的代碼塊和方法一旦被某個線程訪問,則直接鎖住,其他的線程將無法訪問* * 同步代碼塊:* synchronized(鎖對象){* * }* * 注意:鎖對象需要被所有的線程所共享* * * 同步:安全性高,效率低* 非同步:效率高,但是安全性低* */ public class TicketThread implements Runnable {int tickets = 100;//火車票數(shù)量Object obj = new Object();@Overridepublic void run() {//出售火車票while(true) {synchronized (obj) {if(tickets > 0) {try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(Thread.currentThread().getName() + ":" +tickets--);}}}}}測試
public class TicktetTest {public static void main(String[] args) {//創(chuàng)建線程對象TicketThread tt = new TicketThread();Thread t = new Thread(tt);t.setName("窗口1");Thread t2 = new Thread(tt);t2.setName("窗口2");Thread t3 = new Thread(tt);t3.setName("窗口3");//啟動線程對象t.start();t2.start();t3.start();} }使用同步方法解決多線程安全問題
格式:
修飾符 synchronized 返回值 方法名(){
}
案例代碼
/** 同步方法:使用關鍵字synchronized修飾的方法,一旦被一個線程訪問,則整個方法全部鎖住,其他線程則無法訪問* * synchronized* 注意:* 非靜態(tài)同步方法的鎖對象是this* 靜態(tài)的同步方法的鎖對象是當前類的字節(jié)碼對象*/ public class TicketThread implements Runnable {static int tickets = 100;// 火車票數(shù)量Object obj = new Object();@Overridepublic void run() {// 出售火車票while (true) {/*synchronized (obj) {method();}*///method();method2();}}private synchronized void method() {if (tickets > 0) {try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(Thread.currentThread().getName() + ":" + tickets--);}}private static synchronized void method2() {if (tickets > 0) {try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(Thread.currentThread().getName() + ":" + tickets--);}}}代碼測試
public class TicktetTest {public static void main(String[] args) {//創(chuàng)建線程對象TicketThread tt = new TicketThread();Thread t = new Thread(tt);t.setName("窗口1");Thread t2 = new Thread(tt);t2.setName("窗口2");Thread t3 = new Thread(tt);t3.setName("窗口3");//啟動線程對象t.start();t2.start();t3.start();} }總結(jié)
以上是生活随笔為你收集整理的Java多线程安全问题解决的两种方式代码案例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java实现多线程的两种方式
- 下一篇: Spring MVC--接收JSON格式