您好,登錄后才能下訂單哦!
轉載于 : 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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。