转载于 : 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; @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); } } }
转载于 : http://www.verejava.com/?id=16992914422268
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。