所以我有一个创建程序的任务,该程序有一个具有起始余额的银行帐户。然后我创建了一组卡,可以访问该帐户,存入或提取金额,但不能同时。我创建了许多线程,然后使用这些线程访问银行账户,提取或存入随机金额,然后退出。
我的卡片类:
public class card implements Runnable {
private int id;
private account account;
private int count = 1;
card(int id, account acc1){
this.id = id;
this.account = acc1;
}
public int getid(){
return this.id;
}
public int localBalance(){
return account.getBalance();
}
public void run() {
for(count = 0; count <= 5; count++) { //withdraw and deposit random amounts
int transactionamount = (int)(Math.random()*10);
if (Math.random() > 0.5) {
System.out.printf("%-12s%-12s%-12s%-12s\n","card id:(" + getid() + ")"," ",transactionamount, account.getBalance());
account.deposit(transactionamount);
} else {
System.out.printf("%-12s%-12s%-12s%-12s\n","card id:(" + getid() + ")",transactionamount," ",account.getBalance());
account.withdraw(transactionamount);
}
}
}
}
我的帐户类
public class account {
private int balance = 5000;
public account(int balance){
this.balance = balance;
}
public synchronized void withdraw(int amount) {
balance = getBalance() - amount;
}
public synchronized void deposit(int amount) {
balance = getBalance() + amount;
}
public synchronized int getBalance() {
return this.balance;
}
public synchronized void setNewBalance(int localBalance) { //no longer in use
balance = localBalance;
}
}
我的课程类
public class Program {
public static void main(String[] args ) throws InterruptedException {
// TODO Auto-generated method stub
int numberofcards = 5;
int accountbalance = 5000;
card cardArray[] = new card[numberofcards];
account acc1 = new account(accountbalance); //pass into account the starting balance.
System.out.printf("\n%-12s%-12s%-12s%-12s\n","Transaction","Withdrawal","Deposit","Balance");
System.out.printf("%-12s%-12s%-12s%-12s\n"," "," "," ",accountbalance + "\n");
Thread[] threads = new Thread[numberofcards];
for(int i = 1; i < numberofcards; i++ ){
cardArray[i] = new card(i, acc1);
threads[i] = new Thread(cardArray[i]);
threads[i].start();
}
for(int i = 1; i < threads.length; i++ ){
threads[i].join();
}
System.out.printf("\n%-12s%-12s%-12s%-12s\n","finished"," "," ","Final Balance: " + acc1.getBalance());
}
}
输出示例:
http://puu.sh/lvoE5/05ebcd4c74.png
正如你所看到的,它适用于每张不同的卡片,它会回到5000,然后存入或取出一个随机数量。我似乎无法弄清楚为什么我的代码不会不断更新金额。任何帮助都会是惊人的,我很惊讶,无论如何我都是这么远。
编辑:摆脱不需要的新线程.join code&amp;在我的run方法中摆脱了synchronized,输出仍然是相同的,但是请不断指出问题,教自己关于线程真的很难记住。提前谢谢你
编辑2:仍然无法正常工作,现在直接使用提款和存款。链接附加的输出
答案 0 :(得分:0)
您的代码中存在 多个问题 ,首先是您的测试代码永远不会交织,因为我怀疑您希望这样做。
请考虑以下代码:
for(int i = 1; i < numberofcards; i++ ){ //create amount of cards in array of cards
cardArray[i] = new card(i, acc1);
new Thread(cardArray[i]).start();
new Thread(cardArray[i]).join();
}
对于每个循环,您创建两个线程对象(请参阅
new
keyword), 一个你启动线程,另一个你调用join
。 由于第二个线程未运行,it simply returns and does nothing。
此外,由于您的run
方法已同步,因此该类中的任何新synchronized
方法都不会同时运行,这没有任何意义。 请参阅:Should you synchronize the run method? Why or why not?
这可能是你想要的:
Thread threads = new Threads[numberofcards];
for(int i = 1; i < numberofcards; i++ ){
cardArray[i] = new card(i, acc1);
threads[i] = new Thread(cardArray[i]);
threads[i].start();
}
for(int i = 1; i < threads.length; i++ ){
threads[i].join();
}
您使用$ 5000 this.cardBalance = account.getBalance()
初始化每张卡并更新this.cardBalance
。然后,您在设置本地变量期间account.setNewBalance(cardBalance)
但更新帐户,以及您写入帐户的时间,其他线程也获取并更新了帐户的卡余额。< / strong>您对帐户的修改不是原子的。
您不应使用本地变量来表示卡中帐户的余额。这违反了单独的实体规则,没有合法目的。
答案 1 :(得分:-1)
这应该有效,请注意Account类
中的正确同步帐户:
public class Account {
private int bankBalance;
public Account(int initialBankBalance) {
this.bankBalance = initialBankBalance;
}
public synchronized int withdraw(int cardId, int amount) {
bankBalance = bankBalance - amount;
System.out.println("Card: " + cardId + "; withdraw: " + amount + "; after withdrawal: " + bankBalance);
return bankBalance;
}
public synchronized int deposit(int cardId, int amount) {
bankBalance = bankBalance + amount;
System.out.println("Card: " + cardId + "; deposit: " + amount + "; after deposit: " + bankBalance);
return bankBalance;
}
public synchronized int getBankBalance() {
return bankBalance;
}
}
卡:
public class Card implements Runnable {
private int id;
private Account account;
public Card(int id, Account account) {
this.account = account;
this.id = id;
}
@Override
public void run() {
double depositTotal = 0;
double withdrawTotal = 0;
for (int i = 0; i < 5; i++) {
if (Math.random() > 0.5) {
int deposit = (int) (Math.random() * 10);
depositTotal = depositTotal + deposit;
account.deposit(id, deposit);
} else {
int withdraw = (int) (Math.random() * 10);
withdrawTotal = withdrawTotal + withdraw;
account.withdraw(id, withdraw);
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Card: " + id + "; withdrawal total: " + withdrawTotal);
System.out.println("Card: " + id + "; deposit total: " + depositTotal);
}
}
程序:
public class Program {
public static void main(String[] args) throws InterruptedException {
int numCards = 2;
int initialBankBalance = 1000;
Account account = new Account(initialBankBalance);
System.out.println("Starting balance: " + initialBankBalance);
Thread[] threads = new Thread[numCards];
for (int i = 0; i < numCards; i++) {
Thread thread = new Thread(new Card(i+1, account));
thread.start();
threads[i] = thread;
}
for (int i = 0; i < threads.length; i++) {
threads[i].join();
}
System.out.println("End balance is : " + account.getBankBalance());
}
}
样本运行的输出:
Starting balance: 1000
Card: 1; deposit: 3; after deposit: 1003
Card: 2; withdraw: 1; after withdrawal: 1002
Card: 2; deposit: 6; after deposit: 1008
Card: 1; deposit: 3; after deposit: 1011
Card: 1; withdraw: 7; after withdrawal: 1004
Card: 2; deposit: 1; after deposit: 1005
Card: 1; deposit: 0; after deposit: 1005
Card: 2; withdraw: 7; after withdrawal: 998
Card: 1; withdraw: 2; after withdrawal: 996
Card: 2; withdraw: 7; after withdrawal: 989
Card: 2; withdrawal total: 15.0
Card: 1; withdrawal total: 9.0
Card: 2; deposit total: 7.0
Card: 1; deposit total: 6.0
End balance is : 989