我有一个简单的程序模拟N个随机玩家的轮盘游戏时遇到问题。 该程序比意外停止工作2-3次,因为我认为线程仍处于等待状态或类似情况。我知道代码不太好(例如我的英语),但这是我的第一次尝试。任何建议都非常感谢。 抱歉,如果代码不是很干净,我真的找不到其他方法来执行此程序。
public class RouletteMultipla implements Runnable {
private int a;
private int ng; //number of player
public RouletteMultipla(int f) {
ng = f;
}
int numero = 0;
public void ingioco() {
a++; //How many player have put money
}
public void morto() {
System.out.println("One player is died"); //Run out of Money
ng--;
}
public void run() {
while (true) {
synchronized (this) {
if (a == ng) { //until all player have put money
numero = (new Numero().num); //Random from 0 to 36
System.out.println("E' USCITO " + numero + "\n ");
a = 0;
for (double j = 0; j < 10000000; j = j + 0.1);
notifyAll();
}
}
}
}
public static void main(String[] args) {
int ng = (int) (Math.random() * 14) + 1;
RouletteMultipla r = new RouletteMultipla(ng);
Thread roulette = new Thread(r);
for (int i = 0; i < ng; i++) {
Giocatore g = new Giocatore(i, r, ng); //create n player
g.start();
}
roulette.start(); //create one roulette
roulette.setPriority(10);
}
这是类播放器
public class Giocatore extends Thread {
private int id; //id of player
private int ng; //number of player
int soldi = 100; //money at start
int numero; //number
int puntata = 1; //roulette play at start
Giocatore(int i, RouletteMultipla s, int b) {
id = i;
r = s;
ng = b;
}
RouletteMultipla r = new RouletteMultipla(ng);
public void run() {
while (soldi > 0) {
numero = ((int) (Math.random() * 35)) + 1;
puntata = (int) (Math.random() * (soldi - 1)) + 1;
soldi = soldi - puntata;
System.out.println("ID:" + id + " Puntata:" + puntata + " Numero:" + numero + " SOLDI:" + soldi);
r.ingioco(); //Increase "a variable"
try {
synchronized (r) {
r.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
if (numero == r.numero) {
soldi = soldi + puntata * 36;
System.out.println("Giocatore " + id + " HA VINTOOOOOOOOOO");
}
}
r.morto(); // when money=0
}
}
答案 0 :(得分:1)
可能还有其他问题,但对我而言突出的是,对ng
变量的访问既不同步也不是原子的:
public void morto() {
System.out.println("One player is died"); //Run out of Money
ng--;
}
如果多个(玩家)线程同时调用此方法,则不能保证ng--
实际上导致ng
包含正确数目的玩家。
了解问题的最简单方法可能是将n--;
与n = n - 1;
相同。现在,如果两个线程同时运行此线程,它们可能都将n
读为相同的值(例如10),并且都将相同的递减结果存储回去(例如9)。尽管完成了两个线程,但计数仍比应有的计数高一。
您可能应该将方法声明为synchronized
。我认为ingioco
方法也存在类似的问题。