转载于 :http://www.verejava.com/?id=16992914422268

packagecom.thread;importjava.util.Scanner;publicclassTestBank{publicstaticvoidmain(String[]args){Bankbank=newBank();Threadlingming=newThread(bank,"李明");Threadwangtao=newThread(bank,"王涛");lingming.start();try{Thread.sleep(4000);}catch(InterruptedExceptione){e.printStackTrace();}wangtao.start();}}classBankimplementsRunnable{publicstaticintmoney=1000;@Overridepublicsynchronizedvoidrun(){System.out.println(Thread.currentThread().getName()+"登陆您银行的存款为:"+money);try{Thread.sleep(10000);}catch(InterruptedExceptione){e.printStackTrace();}Scannerin=newScanner(System.in);System.out.println("请输入您要取款金额:");intnum=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