如何在 JAVA 中重新创建竞争条件?
How to recreate a race condition in JAVA?
我正在学习 java 多线程并尝试在 JAVA 中创建竞争条件。这是我的代码。
package com.company;
public class Account {
private double balance = 100;
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public boolean withdraw(double amount,String name){
if(this.getBalance()>amount){
this.setBalance(this.getBalance() - amount);
System.out.println(Thread.currentThread().getName() + " withdraw " + amount);
System.out.println("Hello, " + Thread.currentThread().getName() + " You current balance is " + this.getBalance());
return true;
}
else{
System.out.println("Sorry, " + Thread.currentThread().getName() + ". Your current balance is " + this.getBalance() + " and you cannot withdraw " + amount);
//System.out.println("Welcome, " + Thread.currentThread().getName() + " You current balance is " + this.getBalance());
return false;
}
}
}
和主要 class
package com.company;
public class Main implements Runnable {
Account account = new Account();
public static void main(String[] args){
Main main = new Main();
for(int i= 0; i< 2; i++) {
Thread c1 = new Thread(main, "customer" + i);
c1.start();
}
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "'s balance is " + account.getBalance());
account.withdraw(60, Thread.currentThread().getName());
//
}
}
据推测,这应该会产生一个竞速条件,两个客户同时提取 60 美元,getBalance() 应该告诉我每个客户可以提取 60 美元,每个客户的余额为 40 美元。但我永远无法重现这一点。我做错了什么?
重现竞争条件不一定容易。它通常取决于线程调度程序的时间。
您可以通过让其中一个线程休眠来稍微影响它
if (this.getBalance() > amount) {
if (Thread.currentThread().getName().equals("customer0"))
try {
Thread.sleep(1); // simulates a quicker thread context switch
} catch (InterruptedException e) {}
this.setBalance(this.getBalance() - amount);
System.out.println(Thread.currentThread().getName() + " withdraw " + amount);
System.out.println("Hello, " + Thread.currentThread().getName() + " You current balance is " + this.getBalance());
return true;
}
请注意,即使这样也不能保证。它适用于我的系统,可能不适用于您的系统。这就是竞争条件令人讨厌的原因。它们很难一致地复制。
我正在学习 java 多线程并尝试在 JAVA 中创建竞争条件。这是我的代码。
package com.company;
public class Account {
private double balance = 100;
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public boolean withdraw(double amount,String name){
if(this.getBalance()>amount){
this.setBalance(this.getBalance() - amount);
System.out.println(Thread.currentThread().getName() + " withdraw " + amount);
System.out.println("Hello, " + Thread.currentThread().getName() + " You current balance is " + this.getBalance());
return true;
}
else{
System.out.println("Sorry, " + Thread.currentThread().getName() + ". Your current balance is " + this.getBalance() + " and you cannot withdraw " + amount);
//System.out.println("Welcome, " + Thread.currentThread().getName() + " You current balance is " + this.getBalance());
return false;
}
}
}
和主要 class
package com.company;
public class Main implements Runnable {
Account account = new Account();
public static void main(String[] args){
Main main = new Main();
for(int i= 0; i< 2; i++) {
Thread c1 = new Thread(main, "customer" + i);
c1.start();
}
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "'s balance is " + account.getBalance());
account.withdraw(60, Thread.currentThread().getName());
//
}
}
据推测,这应该会产生一个竞速条件,两个客户同时提取 60 美元,getBalance() 应该告诉我每个客户可以提取 60 美元,每个客户的余额为 40 美元。但我永远无法重现这一点。我做错了什么?
重现竞争条件不一定容易。它通常取决于线程调度程序的时间。
您可以通过让其中一个线程休眠来稍微影响它
if (this.getBalance() > amount) {
if (Thread.currentThread().getName().equals("customer0"))
try {
Thread.sleep(1); // simulates a quicker thread context switch
} catch (InterruptedException e) {}
this.setBalance(this.getBalance() - amount);
System.out.println(Thread.currentThread().getName() + " withdraw " + amount);
System.out.println("Hello, " + Thread.currentThread().getName() + " You current balance is " + this.getBalance());
return true;
}
请注意,即使这样也不能保证。它适用于我的系统,可能不适用于您的系统。这就是竞争条件令人讨厌的原因。它们很难一致地复制。