我只需要用java线程做一个竞争条件的例子,我写了这个代码,但我不确定它是否有竞争条件.
有人可以告诉我下面的代码是否有竞争条件,还有我如何改进或简化?
(抱歉英文不好)
public class RaceCondition extends Thread{
static int count=0; //the variable where the race condition need to happen
static int contador1 = 0; //variables to count
static int contador2 = 0;
static Thread t1 = new Thread(new Runnable() {
public void run(){
while(contador1!=20){
count ++;
System.out.print(count + " ");
contador1++;
}
}
});
static Thread t2 = new Thread(new Runnable() {
public void run(){
while(contador2!=20){
int k = 5;
count += 5*k;
System.out.print(count + " ");
contador2 ++; …Run Code Online (Sandbox Code Playgroud)