「取钱java线程」线程银行存取钱
本篇文章给大家谈谈取钱java线程,以及线程银行存取钱对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、如何用Java线程实现银行的存款取款问题最好能写出编出的具体程序
- 2、java 线程同步!银行取钱问题!
- 3、java线程实现存钱就马上取钱
- 4、(java大一题目)多线程
- 5、用java编程实现线程实现多人对同一银行账号的存、取款业务。
如何用Java线程实现银行的存款取款问题最好能写出编出的具体程序
AccountTest.java class BankAccount //定义银行账户类BankAccount{private static int amount =2000; //账户余额最初为2000public void despoit(int m) //定义存款的方法{amount=amount+m;System.out.println("晓明存入["+m+"元]");}public void withdraw(int m) //定义取款的方法{amount=amount-m;System.out.println("张新取走["+m+"元]");if(amount0)System.out.println("***余额不足!***);public int balance() //定义得到账户余额的方法{return amount;}}classicCustomerextendsThread {String name;BankAccount bs; //定义一个具体的账户对象public Customer(BankAccount b,String s){name=s;bs=b;}public static void cus(String name,BankAccount bs) //具体的账户操作方法{if(name.equals("小明")) //判断用户是不是小明{try{for(int i=0;i6;i++) //用户晓明则向银行存款6次,每次1000元 {Thread.currentThread().sleep((int)(Math.random()*300));bs.despoit(1000);}}catch(InterruptedException e){}}else{try{for(int i=0;i6;i++) //用户不是小明则从银行取款6次,每次1000元{Thread.currentThread().sleep((int)(Math.random()*300));bs.withdraw(1000); }}catch(InterruptedException e){} }}public void run() //定义run方法}cus(name,bs); }}public classAccountTest{public static void main(String [] args) throws InterruptedException{BankAccount bs=new BankAccount();Customer customer1=new Customer(bs,"小明");Customer customer2=new Customer(bs,"张新");Thread t1=new Thread(customer1);Thread t2=new Thread(customer2);t1.Start();t2.start();Thread.currentThread().sleep(500);}}
java 线程同步!银行取钱问题!
int i = 5000;
int sum = 0;
你把钱数 i 放在方法里这是一个局部变量
每次调用 都初始化下
方法结束 就没有了
建议你把i放到类的属性里。
这样写
class Money{
volatile int i = 5000;
public void get (){
synchronized (this) {
int sum = 0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入取款金额");
int a = sc.nextInt();
sum = i-a;
System.out.println("余额为"+ sum);
}
}
}
希望我说的清楚
java线程实现存钱就马上取钱
您好,您这样;
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
}
}
(java大一题目)多线程
public class Test {
public static final String mutex = "XXXXXXXXX";
private static int money = 1000;
public static void main(String[] args) {
Thread t1 = new Thread(new myThread("A"));
Thread t2 = new Thread(new myThread("B"));
t1.start();
t2.start();
}
public static class myThread implements Runnable {
private String people;
private int index = 0;
private int total = 0;
public myThread(String people) {
this.people = people;
}
@Override
public void run() {
while(true) {
synchronized (mutex) {
if(money = 0) {
int a=(int)(Math.random()*1000+100)/100*100;
if(money a ) {
System.out.println("余额不足");
break;
}
money -= a;
total += a;
index++;
System.out.println(people + "第" + index + "次取" + a + "元, 总共取了" + total);
}else {
break;
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
}
用java编程实现线程实现多人对同一银行账号的存、取款业务。
你好!
可以参考著名的生产者消费者例子
希望对你有所帮助,望采纳。
取钱java线程的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于线程银行存取钱、取钱java线程的信息别忘了在本站进行查找喔。
发布于:2022-12-13,除非注明,否则均为
原创文章,转载请注明出处。