您好,登錄后才能下訂單哦!
轉(zhuǎn)載于 : 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 + " 您當(dāng)前余額為:" + money); } } }
轉(zhuǎn)載于 : http://www.verejava.com/?id=16992914422268
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。