与多个共享同一对象的线程的Java同步问题

时间:2016-01-26 13:13:38

标签: java multithreading synchronization locking sharing

我目前正在学习Java中的同步。据我所知,同步实例方法获取其对象的锁。

我的程序是一个50个任务的位置,然后给每个线程。每个任务都会为从帐户类创建的Account对象添加一分钱。

帐户类具有余额数据字段和用于存款的同步方法。 50个任务具有指向同一帐户对象的帐户字段(即共享帐户)。一旦调用了run,每个任务都会调用account.deposit实例方法来存入1个单元。

我预计余额将以50个单位结束。令人惊讶的是,该帐户最终有50个或其他余额14,48,33等。

class JavaStudy {
    public static void main(String[] args){

        for (int j = 0; j < 10; j++) {

            Account account = new Account();

            ExecutorService executorPool = Executors.newFixedThreadPool(50);

            for (int i = 0; i < 50; i++) {
                executorPool.execute(new DepositTask(account));
            }

            executorPool.shutdown();

            while(!executorPool.isShutdown()){
            }

            System.out.println(account.getBalance());
        }
    }
}

存款任务类!

class DepositTask implements Runnable {

    private Account account;

    DepositTask(Account account){
       this.account = account;
  }

   @Override
  public void run() {
       account.deposit(1);
  }
}

帐户类!

class Account {

    private int balance = 0;

    synchronized public void deposit(int amount){
        int balance = this.balance + amount;
        this.balance = balance;
    }

    String getBalance(){
        return "Balance: " + balance;
    }
}

根据我的理解,根据我的理解,一旦任务访问account.deposit(1),帐户应该被锁定; 。其他任务不应该能够访问它,因为它们共享同一个对象!不知怎的,这不会发生,我最终会得到以下结果,

Balance: 20
Balance: 47
Balance: 50
Balance: 42
Balance: 27
Balance: 24
Balance: 50
Balance: 29
Balance: 13
Balance: 12

Process finished with exit code 0

有关正在发生的事情的任何想法?

1 个答案:

答案 0 :(得分:5)

我怀疑你没有等待终止,这与关机不同。这可能意味着并未执行所有任务。

executorPool.shutdown();
executorPool.awaitTermination(1, TimeUnit.MINUTES);
System.out.println(account.getBalance());

BTW在Java 8中,您可以使用

简化此操作
Account account = new Account();

InStream.range(0, 50).parallel()
                     .forEach(i -> account.deposit(1));

System.out.println(account.getBalance());