[英]Using thread pool synchronization bank java
我想使用 Java 中的线程制作一个简单的银行。 但是我不能让 deposit(),withdraw() 同步。 始终没有同步平衡。 我在方法名称中写了“同步”关键字,但它从来没有用过。 另外,我在我的 ArrayList 中制作了“synchronizedList”(我应该使用数组列表来制作),但它从来没有用过。 我怎样才能获得适当的平衡? 请帮我。
import java.security.SecureRandom;
public class Transaction implements Runnable {
private static final SecureRandom generator = new SecureRandom();
private final int sleepTime; // random sleep time for thread
private String transaction;
private int amount;
private static int balance;
private Account account = new Account();
public Transaction (String transaction, int amount) {
this.transaction = transaction;
this.amount = amount;
sleepTime = generator.nextInt(2000);
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
if(transaction == "deposit") {
balance = account.deposit(amount);
} else if (transaction == "withdraw") {
balance = account.withdraw(amount);
}
System.out.println("[" + transaction + "] amount : " + amount +" balance : " + balance);
Thread.sleep(sleepTime);
}catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt(); // re-interrupt the thread
}
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class AccountTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// create ArrayList
List<Transaction> john = Collections.synchronizedList(new ArrayList<>());
// add Transaction objects
john.add(new Transaction("deposit", 1000));
john.add(new Transaction("withdraw", 500));
john.add(new Transaction("withdraw", 200));
john.add(new Transaction("deposit", 3000));
// execute Thread Pool
ExecutorService executorService = Executors.newCachedThreadPool();
// start transactions
for(int i=0; i<john.size(); i++) {
executorService.execute(john.get(i));
}
// shut down Thread Pool
}
}
public class Account {
// deposit withdraw
private static int balance;
public synchronized int deposit(int amount) {
balance += amount;
return balance;
}
public synchronized int withdraw(int amount) {
balance -= amount;
return balance;
}
}
这里的核心错误是每个交易都有自己的账户。 每个线程都在自己的 Account 实例上获取锁,结果是没有实际的锁在进行。
您需要线程之间共享的锁,它们需要尝试修改相同的帐户 object。 标记为synchronized
的实例方法获取一个锁在 object 实例中。
在账户 static 上设置余额是一种肮脏的黑客行为,它使所有余额数据最终都放在同一个地方(只有当您只有一个账户时才有效),但不能解决同步问题。
(您也可以将 Account 方法更改为 static,这将解决同步问题,因为所有线程都将获取 class 上的锁定,并且只有一个 class 当然需要停止工作一次。帐户,所以这不是一个很好的解决方案。)
重新进行此操作,以便您在各个事务之间共享相同的帐户 object,而不是让每个事务创建自己的帐户。 您可以将 Account 作为构造函数参数传递给 Transaction。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.