Java 多线程 之 银行ATM实例
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);
}
}
}