java多线程 银行取款
2014-11-26 11:35
323 查看
- package net.okren.java;
- import java.io.*;
- //账户
- class Account{
- private float balance = 1000;
- public float getBalance(){
- return balance;
- }
- public void setBalance(float balance){
- this.balance = balance;
- }
- public synchronized void withDraw(float money){
- if(balance >= money){
- System.out.println("取走 "+ money +"钱");
- try{
- Thread.sleep(1000);
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- balance -= money;
- }else{
- System.out.println("余额不足 ");
- }
- }
- }
- class TestAccount1 extends Thread{
- private Account account;
- public TestAccount1(Account account){
- this.account = account;
- }
- public void run(){
- account.withDraw(800);
- System.out.println("余额为"+ account.getBalance());
- }
- }
- class TestAccount2 extends Thread{
- private Account account;
- public TestAccount2(Account account){
- this.account = account;
- }
- public void run(){
- account.withDraw(700);
- System.out.println("余额为 " + account.getBalance());
- }
- }
- public class JavaTest {
- public static void main(String[] args){
- Account account = new Account();
- TestAccount1 t1 = new TestAccount1(account);
- TestAccount2 t2 = new TestAccount2(account);
- t1.start();
- t2.start();
- }
- }
相关文章推荐
- java面试题-用JAVA中的多线程示例银行取款问题标准版
- java面试题-用JAVA中的多线程示例银行取款问题
- 用JAVA中的多线程示例银行取款问题
- 用JAVA的多线程实现银行取款的问题
- 用JAVA中的多线程示例银行取款问题
- Java-多线程 银行取款小练习
- Java多线程示例——模拟银行柜台处理业务
- 3-3-Java多线程-案例-银行存钱&同步函数
- Java设计的银行取款系统-Mysql数据库
- 银行取款[多线程]{使用volatile修饰共享变量,但此场景并不保证线程同步}
- 银行取款[多线程]{使用重入锁Lock接口ReentrantLock锁确保线程同步}
- Java多线程经典例题之二--银行
- Thinking in Java---多线程仿真:银行出纳员仿真+饭店仿真+汽车装配工厂仿真
- Java多线程简单例子,银行存取示例。
- java多线程-多线程同步代码块-银行存款实例-锁synchronized
- 银行取款[多线程]{使用ThreadLocal管理共享变量,但此场景并不保证线程同步}
- java 线程同步问题之银行存取款问题
- Java多线程之银行出纳员仿真
- Java 多线程 锁 存款 取款
- Java--银行存取款线程设计2(线程互斥)