用java线程同步实现银行取款和存款。

题目是:编写程序实现线程的同步.假设一个银行的ATM机,它可以允许用户存款也可以取款.现在一个账户上有200元,用户A和用户B都拥有在这个账户上存款和取款的权利.用户A将... 题目是:编写程序实现线程的同步.假设一个银行的ATM机,它可以允许用户存款也可以取款.现在一个账户上有200元,用户A和用户B都拥有在这个账户上存款和取款的权利.用户A将存入100元,而用户B将取出50元,那么最后账户的存款应该是250元。求代码
那位大哥大姐会做啊?我的是
//利用线程同步实现模拟银行取款和存款
class Account
{
private static int cunqian = 200;
String name;
static float amount;
static float bmount;
public Account(String name,float amount,float bmount)
{
this.name=name;
this.amount=amount;
this.bmount=bmount;
}
public synchronized static void deposit(float amt)
{
float tmp = amount;
tmp += amt;

try
{
Thread.sleep(10);//模拟其它处理所需要的时间,比如刷新数据库等
}
catch (InterruptedException e)
{

}

amount = tmp;
}

public synchronized static void withdraw(float bmt)
{
float tmp = bmount;
tmp -= bmt;

try
{
Thread.sleep(10);//模拟其它处理所需要的时间,比如刷新数据库等
}
catch (InterruptedException e)
{

}

bmount = tmp;
}
public static float getBalance()
{

return (amount+bmount);

}
}
class Customer extends Thread
{ public void run()
{
for(int i=0;i<=1;i++)

Account.deposit(100.0f);
Account.withdraw(50.0f);
}

}

public class AccountTest
{
public static void main(String[] args)
{
Customer customerA=new Customer();
Customer customerB=new Customer();
customerA.start();
customerB.start();
System.out.println();
System.out.println("最后总钱数:" + Account.getBalance());

}

}
运行结果为0,谁能帮我改正一下。
展开
 我来答
dxr528
推荐于2017-09-08 · 超过28用户采纳过TA的回答
知道小有建树答主
回答量:48
采纳率:0%
帮助的人:68万
展开全部
你的代码问题很多!
帮你修改后:
class Account {

private static float Balance = 200f;//线程共享的钱

public synchronized static void deposit(float amt) {
Balance = Balance + amt;
try {
Thread.sleep(10);// 模拟其它处理所需要的时间,比如刷新数据库等
}
catch (InterruptedException e) {

}

}

public static float getBalance() {
return Balance;
}

public static void setBalance(float balance) {
Balance = balance;
}

public synchronized static void withdraw(float bmt) {
Balance -= bmt;
try {
Thread.sleep(10);// 模拟其它处理所需要的时间,比如刷新数据库等
}
catch (InterruptedException e) {

}
}

String name;

public Account(String name) {
this.name = name;
}
}

public class AccountTest {
public static void main(String[] args) {
Customer customerA = new Customer("A");
Customer customerB = new Customer("B");
customerA.start();
customerB.start();
System.out.println();
try {
customerA.join();//等待A线程执行完毕
customerB.join();//等待B线程执行完毕
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("最后总钱数:" + Account.getBalance());

}
}

class Customer extends Thread {

public Customer(String string) {
super(string);
}

@Override
public void run() {
System.out.println(currentThread().getName());
if (currentThread().getName().equals("A"))
Account.deposit(100.0f);//A线程加100
else if (currentThread().getName().equals("B"))
Account.withdraw(50.0f);//B线程减50

}

}
ljs120ljs
2010-06-11 · 超过27用户采纳过TA的回答
知道答主
回答量:107
采纳率:0%
帮助的人:51.7万
展开全部
package help;

class Account
{
private static int cunqian = 200;
String name;

public Account(String name)
{
this.name = name;
}

public synchronized static void deposit(float amt)
{
cunqian += amt;

try
{
Thread.sleep(10);// 模拟其它处理所需要的时间,比如刷新数据库等
}
catch (InterruptedException e)
{

}

}

public synchronized static void withdraw(float bmt)
{
cunqian -= bmt;

try
{
Thread.sleep(10);// 模拟其它处理所需要的时间,比如刷新数据库等
}
catch (InterruptedException e)
{

}
}

public static float getBalance()
{

return cunqian;

}
}

class Customer extends Thread
{
public void run()
{
for (int i = 0; i <= 1; i++)//这只话你是增加200 取走50
Account.deposit(100.0f);
Account.withdraw(50.0f);
}

}

public class AccountTest
{
public static void main(String[] args)
{
Customer customerA = new Customer();
Customer customerB = new Customer();
customerA.start();
customerB.start();
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println();
System.out.println("最后总钱数:" + Account.getBalance());

}

}

//执行结果是500.0
原因
for (int i = 0; i <= 1; i++)//这只话你是增加200 取走50 余150*2 = 300
再加余额200 一共500
注意事项
要在start()后加上如下代码
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
那是因为线程没有执行完你就计算了结果 所以在等待线程执行
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式