When I tried to compile this code, it has one error says
ThreadsDemo.java:37: 'else' without 'if'
else {
^
I don't know what's wrong with that exactly?
import java.util.*;
class A extends Thread {
private static int counter=0;
private final int id=++counter;
A() { start(); }
public void run() {
synchronized (this) {
Random randomNumbers = new Random();
int face;
for (int count = 1; count <= 20; count++) {
face = 1 + randomNumbers.nextInt(500); }
while(true) {
System.out.print("["+id+"] ");
try {
wait(face);
} catch(InterruptedException e) {
System.out.print("\nThread ["+id+"] interrrupted");
break;
}
}
}
}
class ThreadsDemo {
public static void main(String[] args) {
ArrayList list=new ArrayList();
for(int i=0; i<5; i++) list.add(new A());
try {Thread.sleep(3000);
} catch(InterruptedException e) { };
for(int i=0; i<list.size(); i++)
((A) (list.get(i))).interrupt();
for(int i=0; i<list.size(); i++) {
if ( (A) (list.get(i)).isAlive()) {
System.out.println("Continue."); } }
else {
System.out.println("---All threads now ended---"); }
}
}
}