I get the right output but it doesn't pass
Smith:Start game
Jones:Start game
Gates:Start game
Gates:Gather resources
Smith:Gather resources
Gates:Grow economy
Gates:Kill enemies
Smith:Grow economy
Gates:won!
Jones:lost!
Smith:lost!
package com.codegym.task.task16.task1627;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;
public class Solution {
public static void main(String[] args) throws InterruptedException {
OnlineGame onlineGame = new OnlineGame();
onlineGame.start();
}
public static class OnlineGame extends Thread {
public static volatile boolean isWinnerFound = false;
public static List<String> actions = new ArrayList<>();
static {
actions.add("Start game");
actions.add("Gather resources");
actions.add("Grow economy");
actions.add("Kill enemies");
}
protected Gamer gamer1 = new Gamer("Smith", 3);
protected Gamer gamer2 = new Gamer("Jones", 1);
protected Gamer gamer3 = new Gamer("Gates", 5);
public void run() {
gamer1.start();
gamer2.start();
gamer3.start();
//try{Thread.sleep(6000);}catch(InterruptedException e){}
//isWinnerFound = true;
while (!isWinnerFound) {
}
gamer1.interrupt();
gamer2.interrupt();
gamer3.interrupt();
}
}
public static class Gamer extends Thread {
private int rating;
public Gamer(String name, int rating) {
super(name);
this.rating = rating;
}
@Override
public void run() {
//write your code here
//try{Thread.sleep(1000);}catch(InterruptedException e){}
//for (int i =0 ; i < OnlineGame.actions.size();i++){
// System.out.println(Thread.currentThread().getName() +" : "+ OnlineGame.actions.get(i));
//
// if(!currentThread().isInterrupted() && i == OnlineGame.actions.size()-1){
// System.out.println(currentThread().getName() + " :won!");
// OnlineGame.isWinnerFound = true;
// }
//
// if(currentThread().isInterrupted()) {
// System.out.println(currentThread().getName() + " :lost!");
// break;
// }
//}
for (int i = 0; i<OnlineGame.actions.size(); i++){
if (OnlineGame.isWinnerFound) {
System.out.println(currentThread().getName() + ":" + "lost!");
break;
}else if(!OnlineGame.isWinnerFound ){
System.out.println(currentThread().getName() + ":"+OnlineGame.actions.get(i));
try { Thread.sleep((long) 1000 / rating); } catch (InterruptedException e) {}
}
if(!OnlineGame.isWinnerFound && i == OnlineGame.actions.size() -1 ){
OnlineGame.isWinnerFound = true;
System.out.println(currentThread().getName() + ":" + "won!");
}
}
}
}
}