, :
0A, 0B, 0C, 0D, 0E, 1A, 1B, 1C, 1D, 1E, 2A, 2B, 2C, 2D, 2E, stop 10
11
12
13
14
15
, sth, 0A, 1A (A 3000 ), 3 .. A, B, C, D, E, A, A, A, A, B, B, B....
But also I just canβt kill these threads, the cycle never ends, I would like to kill it until Main dies.
Thank you for all your comments.
import java.util.ArrayList;
public class Main extends Thread {
ArrayList<Thread> threads;
public Main() {
super();
threads = new ArrayList<Thread>();
}
public void run(){
Object monitor = new Object();
for (int i = 0; i <= 5; i++) {
threads.add(new Thread(new Task(i + 65, monitor)));
}
long cT = System.currentTimeMillis();
for (int i = 0; i < threads.size(); i++) {
threads.get(i).start();
}
synchronized(monitor){
while (System.currentTimeMillis() - cT < 10000) {
try{
monitor.notify();
Thread.sleep(50);
monitor.wait();}catch(Exception e){e.printStackTrace();}
}
for(int i = 0; i < threads.size(); i++){
System.out.println("I suspend "+threads.get(i).getId());
threads.get(i).stop();
}
}
}
public static void main(String[] args) {
new Main().start();
}
}
N
public class Task implements Runnable {
int nr;
char character;
Object monitor;
public Task(int literaASCII, Object monitor) {
this.nr = 0;
this.monitor = monitor;
character = (char) (literaASCII);
}
@Override
public void run() {
synchronized (monitor) {
while (true) {
try {
monitor.wait();
System.out.print(nr + "" + character + ", ");
nr++;
int r = (int) ((Math.random() * 500) + 500);
Thread.sleep(r);
} catch (Exception e) {
e.printStackTrace();
}
monitor.notify();
}
}
}
}