java多線程實現(xiàn)取款小程序
最近java學到了多線程編程中的線程同步問題,教材中的取款小程序?qū)τ诶斫饩€程和線程同步很有幫助,在此將其補充完善.
1、建立java類

這里建立了三個java類,第一個Account類,用于封裝賬戶中的各種信息;第三個DrawThread類用于實現(xiàn)線程體;第二個類主要封裝了主函數(shù)
2、Account類
public class Account {
? ? //封裝賬戶編號,賬戶余額和兩個成員變量
? ? private String accountNo;
? ? private double balance;
? ? public Account(){};
? ? public Account(String accountNo,double balance){
? ? ? ? this.accountNo=accountNo;
? ? ? ? this.balance=balance;
? ? }
? ? public void setAccountNo(String accountNo)
? ? {
? ? ? ? this.accountNo=accountNo;
? ? }
? ? public void setBalance(double balance){
? ? ? ? this.balance=balance;
? ? }
? ? public String getAccountNo(){
? ? ? ? return accountNo;
? ? }
? ? public double getBalance(){
? ? ? ? return balance;
? ? }
? ? public int hashCode(){
? ? ? ? return accountNo.hashCode();
? ? }
? ? public boolean equals(Object obj){
? ? ? ? if(this==obj){
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? if (obj!=null&&obj.getClass()==Account.class){
? ? ? ? ? ? Account target=(Account)obj;
? ? ? ? ? ? return target.getAccountNo().equals(accountNo);
? ? ? ? }
? ? ? ? return false;
? ? }
}3、DrawThread類
public class DrawThread extends Thread {
? ? //模擬用戶賬戶
? ? private Account account;
? ? //當前取錢線程所希望的取錢數(shù)
? ? private double drawAmount;
? ? public DrawThread(String name, Account account, double drawAmount) {
? ? ? ? super(name);
? ? ? ? this.account = account;
? ? ? ? this.drawAmount = drawAmount;
? ? }
? ? //當多個線程修改同一個共享數(shù)據(jù)時,將涉及數(shù)據(jù)安全問題
? ? public void run() {
? ? ? ? //使用account作為同步監(jiān)視器,任何線程進入下面同步代碼塊之前
? ? ? ? //必須先獲得對account賬戶的鎖定——其他線程無法獲得鎖,也就無法修改它
? ? ? ? synchronized (account) {
? ? ? ? ? ? if (account.getBalance() >= drawAmount) {
? ? ? ? ? ? ? ? //吐出鈔票
? ? ? ? ? ? ? ? System.out.println(getName() + "取錢成功!吐出鈔票:" + drawAmount);
? ? ? ? /*
? ? ? ? try {
? ? ? ? ? ?Thread.sleep(1);
? ? ? ? ? ?}catch (InterruptedException ex)
? ? ? ? {
? ? ? ? ? ? ex.printStackTrace();
? ? ? ? }
? ? ? ? */
? ? ? ? ? ? ? ? //修改余額
? ? ? ? ? ? ? ? account.setBalance(account.getBalance() - drawAmount);
? ? ? ? ? ? ? ? System.out.println("\t余額為:" + account.getBalance());
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? System.out.println(getName() + "取錢失??!余額不足!");
? ? ? ? ? ? }
? ? ? ? }
? ? }
}4、DrawTest類
public class DrawTest {
? ? public static void main(String[] args){
? ? ? ? //創(chuàng)建一個賬戶
? ? ? ? Account acct=new Account("1234567",1000);
? ? ? ? //模擬兩個線程對同一個賬戶取錢
? ? ? ? new DrawThread("jack",acct,800).start();
? ? ? ? new DrawThread("rose",acct,800).start();
? ? }
}運行結(jié)果:

以上程序有一點需要注意,就是用到了同步代碼塊。它可以解決run()方法不具備同步安全性(即當兩個線程同時發(fā)送請求時即會造成異常)
同步代碼塊:
synchronized (obj){
//需要執(zhí)行的代碼
}如果我們?nèi)サ魊un()方法中的同步代碼塊,繼續(xù)運行
Account acct=new Account("1234567",1000);
new DrawThread("jack",acct,800).start();
new DrawThread("rose",acct,800).start();(銀行賬戶一共1000元,Jack和rose分別對同一賬戶取錢)
運行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud Gateway的基本入門和注意點詳解
這篇文章主要介紹了SpringCloud Gateway的基本入門和注意點,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringBoot整合XxlJob分布式任務調(diào)度平臺
xxl-job是一個開源的分布式定時任務框架,它可以與其他微服務組件一起構(gòu)成微服務集群。它的調(diào)度中心(xxl-job)和執(zhí)行器(自己的springboot項目中有@XxlJob("定時任務名稱")的方法)是相互分離,分開部署的,兩者通過HTTP協(xié)議進行通信2023-02-02
IDEA?mybatis?Mapper.xml報紅的最新解決辦法
這篇文章主要介紹了IDEA?mybatis?Mapper.xml報紅的解決辦法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
springboot使用DynamicDataSource動態(tài)切換數(shù)據(jù)源的實現(xiàn)過程
這篇文章主要給大家介紹了關(guān)于springboot使用DynamicDataSource動態(tài)切換數(shù)據(jù)源的實現(xiàn)過程,Spring Boot應用中可以配置多個數(shù)據(jù)源,并根據(jù)注解靈活指定當前使用的數(shù)據(jù)源,需要的朋友可以參考下2023-08-08

