转载于 : Http://www.verejava.com/?id=16992914422268 package com.thread;import java.util.Scanner;public&
转载于 : Http://www.verejava.com/?id=16992914422268
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;@Overridepublic 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);}}}
转载于 : http://www.verejava.com/?id=16992914422268
--结束END--
本文标题: Java 多线程 之 银行ATM实例
本文链接: https://lsjlt.com/news/231417.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0