银行java多线程例子_Java 多线程 之 银行ATM实例
package com.thread;
import java.util.Scanner;
public class TestBank {
public static void main(String[] args) {
Bank bank = new Bank();
Thread lingming = new Thread(bank, "李明");
Thread wangtao = new Thread(bank, "王濤");
lingming.start();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
wangtao.start();
}
}
class Bank implements Runnable {
public static int money = 1000;
@Override
public synchronized void run() {
System.out.println(Thread.currentThread().getName() + " 登陸 您銀行的存款為:" + money);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Scanner in = new Scanner(System.in);
System.out.println("請輸入您要取款金額:");
int num = in.nextInt();
if (money < num) {
System.out.println(Thread.currentThread().getName() + "您的余額:" + money + " 不足 " + num);
} else {
money = money - num;
System.out.println(Thread.currentThread().getName() + " 取了 " + num + " 您當前余額為:" + money);
}
}
}
總結(jié)
以上是生活随笔為你收集整理的银行java多线程例子_Java 多线程 之 银行ATM实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kaldi在java中运行_ubuntu
- 下一篇: java中8进制常量_下列关于Java语