I want to create a class DisplayChar, which creates 16 threads and waits for it to finish. Every thread returns a different letter and should be returned 100 times.
I tried to use an array in which I save all threads, start them and then wait for them to finish with "join".
I know that I am doing this wrong, but how can I fix this? How can I use a for-loop to count up the letter and at the same time use a for-loop to create and start the threads?
String letter;
int n;
//constructor
DisplayChar(String Letter, int N){
this.letter= Letter;
this.n = N;
}
public void run() {
for (int i= 1; i<=n; i++) {
System.out.println(""+i+letter);
System.out.flush();
}
}
public static void main(String[] args) {
Thread threads[] = new Thread[16];
for (char x = 'a'; x <= 'p'; x++) {
for (int j = 0; j < threads.length; j++) {
threads[j] = new Thread (new DisplayChar( ""+x, 100));
}
threads[j].start();
}
for (int j = 0; j < threads.length; j++) {
try {
threads[j].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}