第四周作业-多线程编程
来源:互联网 发布:淘宝绫致时装官方店是 编辑:程序博客网 时间:2024/05/16 14:59
class BankAccount{private static int amount=2000;public void despoit(int m){amount =amount + m;System.out.println("小明存入["+m +"元]");}public void withdraw(int m){amount =amount-m;System.out.println("张新取走["+m +"元]");if (amount <0)System.out.println("***余额不足!***");}public int balance(){return amount;}}class Customer extends Thread{String name;BankAccount bs;public Customer(BankAccount b,String s){name =s;bs =b;}public synchronized static void cus (String name, BankAccount bs){if(name.equals("小明")){try{for(int i =0;i<6;i++){Thread.currentThread();Thread.sleep((int)(Math.random()*300));bs.despoit(1000);}}catch(InterruptedException e){}}else{try{for(int i=0;i<6;i++){Thread.currentThread();Thread.sleep((int)(Math.random()*300));bs.withdraw(1000);}}catch(InterruptedException e){}}}public void run(){cus(name,bs);}}public class AccountTest1{public static void main(String args[])throws InterruptedException{BankAccount bs =new BankAccount();Customer customer1=new Customer(bs,"小明");Customer customer2=new Customer(bs,"张新");Thread t1 =new Thread(customer1);Thread t2 =new Thread(customer2);t1.start();t2.start();Thread.currentThread();Thread.sleep(500);}}
0 0
- 第四周作业-多线程编程
- 第四周作业-多线程编程
- 第四周作业--多线程编程
- 第四周作业-多线程编程
- 第四周作业-多线程编程
- 第四周作业-多线程编程
- 第四周作业-多线程编程
- 第四周作业-多线程编程
- 第四周作业-多线程编程
- 第四周作业 多线程编程
- 第四周作业-多线程编程
- 第四周作业-多线程编程
- 第四周作业-多线程编程
- 第四周作业-多线程编程
- 第四周作业-多线程编程
- 第四周作业-多线程编程
- 第四周作业-多线程编程
- 第四周作业:多线程
- Fiddler 用来做反向代理抓包
- 一口一口吃掉struts(七)——ActionForward知多少 .
- 单例模式-多线程单例模式
- 线闭合问题
- WebService简单案例
- 第四周作业-多线程编程
- 初学jsp(2)————————部署简单的Web应用
- iOS开发-点击屏幕,键盘消失的极佳方法。
- Fedora 13安装samba所遇到的问题与解决办法分享
- 残酷月光
- 重新开始战斗07-编程之美-高效见面会
- 两个泛型的实现方式
- WWJC临近 大批国外网友召唤iOS 7.1越狱
- 手机短信的PDU编码和解码