多线程银行帐户Java

时间:2018-10-23 20:04:13

标签: java multithreading thread-safety

我正在尝试使用一个线程运行的银行帐户应用程序。 我想拥有3个不同的银行帐户,但是我不确定该怎么做。

我的代码如下:

package bankapp1;

public class Account implements Runnable {
  int balance;
  int preTransaction;
  String customerName;
  String customerId;

  Account()
  {
     balance = 6000;
  }

  public void run() {
      for (int i =1; i <=4; i++) {
          deposit(2000);
          if (getBalance() < 0 ) {
              System.out.println("account is overdrawn!");
          }
      }

  }
  public synchronized void deposit (int amount) {
      if (getBalance() >= amount ) {
          System.out.println(Thread.currentThread().getName() + " is going to withdraw $ "
          + amount);
          try {
              Thread.sleep(3000);
          } catch (InterruptedException ex) {
          }
          withdraw(amount);
          System.out.println(Thread.currentThread().getName() + " completes the withdrawl of $ "
          + amount);
      } else {
          System.out.println(" not enought in account for " + Thread.currentThread().getName()
                  + "to withdraw" + getBalance());
      }
  }


  public int getBalance() {
      return balance;
  }

  public void withdraw(int amount) {
      if (amount!=0) {
      balance = balance - amount;
      preTransaction = -amount;
      }
  }

  void getPreTransaction()
  {
      if (preTransaction > 0)
      {
          System.out.println("Deposited: " +preTransaction);
      }
      else if (preTransaction < 0) {
          System.out.println("Withdrawn: " + Math.abs(preTransaction));
      }
      else {
          System.out.println("No transaction occured");
      }
  }

}

package bankapp1;

public class ClientTesting {

    public static void main(String[] args) {
        Account acc = new Account();
        Thread t1 = new Thread(acc);
        Thread t2 = new Thread(acc);
        t1.setName("pinelopi");
        t2.setName("andreas");

        t1.start();
        t2.start();

    }
}

我是否必须创建另一个类,名为accountingAccount,其实现方式与Account类几乎相同,然后像在Account类中那样在ClientTesting类中调用它?

2 个答案:

答案 0 :(得分:0)

尝试这样的事情。

 public class Account1 implements Runnable {
 @Override
 public void run() {
    try {

    //logic here

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
 }

}

   public class Account2 implements Runnable
   public class Account3 implements Runnable

   public interface ThreadFactory {
        Thread newTread(Runnable runnable);
     }

    public class MyThreadFactory implements ThreadFactory {
     private String name;
     public MyThreadFactory(String name){
       this.name = name;
       }

    @Override
    public Thread newTread(Runnable runnable) {
     Thread t = new Thread(runnable);
    }

    // here addition logic (deposit, getBalance, ...)


    public class Main {
       public static void main(String[] args) {
       MyThreadFactory factory=new MyThreadFactory("MyThreadFactory");
       Thread th1 = factory.newTread(new Account1());
       Thread th2 = factory.newTread(new Account2());   
       Thread th3 = factory.newTread(new Account3());   
       th1.start();
       th2.start();
       th3.start();

答案 1 :(得分:0)

您可以像这样将Runnable任务提交给ExecutorService

ExecutorService execService = null;
try {
    Account acc = new Account();
    execService = Executors.newFixedThreadPool(2);
    execService.submit(acc);
    execService.submit(acc);
    execService.submit(acc);
} finally {
    execService.shutdown();
}