多线程环境下的线程不安全问题(1)
生活随笔
收集整理的這篇文章主要介紹了
多线程环境下的线程不安全问题(1)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在不考慮多線程的情況下,很多類代碼都是完全正確的,但是如果放在多線程環境下,這些代碼就很容易出錯,我們稱這些類為 線程不安全類 。多線程環境下使用線程安全類 才是安全的。
下面是一個線程不安全類的例子:
public class Account {?????private Integer balance;
?????public Account(Integer balance) {?????????? super();?????????? this. balance = balance;?????}
?????public Integer getBalance() {?????????? return balance;?????}
?????public void setBalance(Integer balance) {?????????? this. balance = balance;?????}??????????public void draw(Integer drawAccount){?????????? if( balance>= drawAccount){???????????????System. out.println(Thread. currentThread().getName()+"取錢成功,吐出鈔票:" +drawAccount );?????????????? balance-= drawAccount;??????????????System. out.println( "余額為:"+balance );??????????} else{??????????????System. out.println(?Thread.?currentThread().getName()+"余額不足,取錢失敗!" );??????????}?????}}
public class DrawThread extends Thread{?????private Account account;?????private Integer drawAccount;?????public DrawThread(String name,Account account, Integer drawAccount) {?????????? super( name);?????????? this. account = account;?????????? this. drawAccount = drawAccount;?????}?????//當多條線程共享一個數據的時候,會涉及到線程安全問題?????public void run(){?????????? account.draw( drawAccount);?????}}
public class Main{?????public static void main(String[] args) {??????????Account account = new Account(1000);?????????? //模擬兩條線程對于同一個賬戶取錢?????????? new DrawThread( "甲", account, 800).start();;?????????? new DrawThread( "乙", account, 800).start();;?????}}
運行結果:
很明顯線程同步時發生了問題,線程不安全。
那么如何解決呢?下一節講。
來自為知筆記(Wiz)
下面是一個線程不安全類的例子:
public class Account {?????private Integer balance;
?????public Account(Integer balance) {?????????? super();?????????? this. balance = balance;?????}
?????public Integer getBalance() {?????????? return balance;?????}
?????public void setBalance(Integer balance) {?????????? this. balance = balance;?????}??????????public void draw(Integer drawAccount){?????????? if( balance>= drawAccount){???????????????System. out.println(Thread. currentThread().getName()+"取錢成功,吐出鈔票:" +drawAccount );?????????????? balance-= drawAccount;??????????????System. out.println( "余額為:"+balance );??????????} else{??????????????System. out.println(?Thread.?currentThread().getName()+"余額不足,取錢失敗!" );??????????}?????}}
public class DrawThread extends Thread{?????private Account account;?????private Integer drawAccount;?????public DrawThread(String name,Account account, Integer drawAccount) {?????????? super( name);?????????? this. account = account;?????????? this. drawAccount = drawAccount;?????}?????//當多條線程共享一個數據的時候,會涉及到線程安全問題?????public void run(){?????????? account.draw( drawAccount);?????}}
public class Main{?????public static void main(String[] args) {??????????Account account = new Account(1000);?????????? //模擬兩條線程對于同一個賬戶取錢?????????? new DrawThread( "甲", account, 800).start();;?????????? new DrawThread( "乙", account, 800).start();;?????}}
運行結果:
很明顯線程同步時發生了問題,線程不安全。
那么如何解決呢?下一節講。
來自為知筆記(Wiz)
轉載于:https://www.cnblogs.com/ZhangJinkun/p/4531692.html
總結
以上是生活随笔為你收集整理的多线程环境下的线程不安全问题(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ActionResult的其它返回值
- 下一篇: 寻找道路(codevs 3731)题解