【发布时间】:2017-05-04 01:31:24
【问题描述】:
这与我之前提出的问题的代码相同,但解决了不同的问题。本质上,我正在尝试使用两个线程创建一个银行帐户,每个线程代表该帐户的用户。用户将从账户中存取 20 美元(随机)。
但是,这两个线程并行运行,并且提取/存款同时发生。我试图限制两个线程,使其在执行其之前等待另一个线程完成自己的运行方法。
下面列出的是代码。
线程创建类
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
public class BankAccount extends Thread{
public static double balance = 1000;
public String threadName;
BankAccount(String name){
threadName = name;
}
public void run(){
System.out.println(threadName + "account initiated.");
for(int i = 0; i < 10; i++){
try{
Random rand = new Random();
int num = rand.nextInt(2) + 1;
if(num == 1){
Thread.sleep(200); //0.2 seconds to deposit
System.out.println(threadName + " is depositing 20$ in the bank.");
balance += 20;
System.out.println("The new balance is " + balance + "dollars" );
}
else{
Thread.sleep(500); //half a second to withdraw
System.out.println(threadName + " is withdrawing 20$ from the bank.");
balance -= 20;
System.out.println("The new balance is " + balance + "dollars.");
}
}
catch(InterruptedException e){
System.out.println("Process terminated.");
}
}
}
}
线程驱动类
public class BankAccountSimDriver {
public static void main(String[] args){
Thread user1 = new BankAccountSIm("user1");
Thread user2 = new BankAccountSIm("user2");
user1.start();
user2.start();
}
}
当前输出:
user1 initiated.
user2 initiated.
user1 is depositing 20$ in the bank.
user2 is depositing 20$ in the bank.
The new balance is 1020.0 dollars
The new balance is 1040.0 dollars
user2 is depositing 20$ in the bank.
The new balance is 1060.0 dollars
user1 is withdrawing 20$ from the bank.
The new balance is 1040.0 dollars.
目前,user1 和 user2 同时运行。我想编辑代码,以便一次只有一个用户可以存款/取款(由 sleep() 时间分隔表示)
所以理想的输出:
user1 initiated.
//wait 0.2 seconds
user1 is depositing 20$ in the bank.
The new balance is 1020.0 dollars
user2 initiated.
//wait 0.2 seconds
user2 is depositing 20$ in the bank.
The new balance is 1040.0 dollars
//wait 0.5 seconds
user1 is withdrawing 20$ from the bank.
The new balance is 1020.0 dollars.
...
【问题讨论】:
-
如果不想让线程同时运行,为什么还要使用线程?
-
你是在一个线程中根据随机值进行存款和取款。那你为什么要启动第二个线程 user2.start()
-
我使用线程来模拟两个不同的人在一个帐户上执行操作,一次一个。有没有更简单或更有效的替代方案? (如您所知,Java 新手)
-
不要在每次使用时实例化您的
Random实例。这扼杀了它的“随机”特性。实例化一次,让它处理生成新值。 -
您可以考虑在代码的关键部分周围使用Java 的许多同步机制之一。您是否阅读过任何有关 Java 并发编程的教程、书籍或其他培训材料?我猜不是因为缺乏同步和滥用
InterruptedException。
标签: java multithreading