一次运行一个线程(而不是并行)

时间:2017-05-03 17:06:41

标签: java multithreading

这与我之前提出的问题相同,但解决了另一个问题。基本上我正在尝试创建一个有两个线程的银行账户,每个线程代表一个帐户的用户。用户将从账户中存入和取出20美元(随机)。

然而,这两个线程并行运行,撤销/存款同时发生我试图限制两个线程,以便在执行其之前等待另一个线程完成自己的运行方法。

下面列出的是代码。

线程创建类

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

public class BankAccount extends Thread{
public static double balance = 1000;
public String threadName;

BankAccount(String name){
    threadName = name;
}
public void run(){
    System.out.println(threadName + "account initiated.");
    for(int i = 0; i < 10; i++){
        try{
            Random rand = new Random();
            int num = rand.nextInt(2) + 1;
            if(num == 1){
                Thread.sleep(200); //0.2 seconds to deposit
                System.out.println(threadName + " is depositing 20$ in the bank.");
                balance += 20;
                System.out.println("The new balance is " + balance + "dollars" );
            }
            else{
                Thread.sleep(500); //half a second to withdraw
                System.out.println(threadName + " is withdrawing 20$ from the bank.");
                balance -= 20;
                System.out.println("The new balance is " + balance + "dollars.");
                }
        }
        catch(InterruptedException e){
            System.out.println("Process terminated.");
            }
        }
    }
}

线程驱动程序类

public class BankAccountSimDriver {
    public static void main(String[] args){
    Thread user1 = new BankAccountSIm("user1"); 
    Thread user2 = new BankAccountSIm("user2");

    user1.start();
    user2.start();
    }
}

目前的输出:

user1 initiated.
user2 initiated.
user1 is depositing 20$ in the bank.
user2 is depositing 20$ in the bank.
The new balance is 1020.0 dollars
The new balance is 1040.0 dollars
user2 is depositing 20$ in the bank.
The new balance is 1060.0 dollars
user1 is withdrawing 20$ from the bank.
The new balance is 1040.0 dollars.

目前,user1和user2同时运行。我想编辑代码,以便一次只能有一个用户存款/取款(由睡眠()时间分隔表示)

所以理想的输出:

user1 initiated.
//wait 0.2 seconds
user1 is depositing 20$ in the bank.
The new balance is 1020.0 dollars
user2 initiated.
//wait 0.2 seconds
user2 is depositing 20$ in the bank.
The new balance is 1040.0 dollars
//wait 0.5 seconds
user1 is withdrawing 20$ from the bank.
The new balance is 1020.0 dollars.
...

1 个答案:

答案 0 :(得分:2)

您需要的是管理balance的并发访问,而不是序列化线程,至少需要序列化对balance的访问。有很多方法可以解决它。基本上,你需要一个 lock ,只允许一个线程修改balance,另一个被阻塞,直到另一个释放锁。

首先,您需要将BanckAccount与其用户分开。

class BankAccount { ... }

class User implements Runnable {
    private BankAccount account;
    public User(BankAccount account) {
         this.account = account;
    }
    public void run() {
      // do something...
    }
}

class Test {
    public static void main(String []a) {
        // One account
        BankAccount a = new BankAccount();
        // Shared by 2 users
        User user1 = new User(a);
        User user2 = new User(a);
        // Make users behave concurrently
        Thread t1 = new Thread(user1);
        Thread t2 = new Thread(user2);
        t1.start();
        t2.start();
        // Wait 'til the end of the users activity
        t1.join();
        t2.join();
    }
}

现在如何管理用户对同一银行帐户的并发访问?您可以通过BankAccount方法BankAccount使用synchronized对象作为隐式锁定:

class BankAccount {
    private int balance;
    public BankAccount(int initialBalance) {
        balance = initialBalance;
    }
    public synchronized int deposit(int amount) {
        balance += amount;
        return balance;
    }
    public synchronized int withdraw(int amount) {
        balance -= amount;
        return balance;
    }
}

这样做可以确保在标记为synchronized的方法中不能同时存在两个线程,因此没有两个线程可以同时修改余额。

但是......(并发导致细微问题)......每个线程都可能使用balance的缓存副本,因此通常更适合将该字段标记为volatile

private volatile int balance;

请注意,在这种简单的情况下,某些事情可能会被简化,但我试图向您展示有哪些问题以及如何解决这些问题。还要注意,即使这段代码也可能导致奇怪的输出,但保证是平衡是正确的(现在就挖掘并发性)。