So I am having an issue understanding how to avoid sequential execution of threads. I am attempting to create an array of threads and execute the start() and join() functions in separate loops. Here's a code example of what I have now:
private static int[] w;
static class wThreads implements Runnable {
private final int i;
public wThreads(int i) {
this.i = i;
}
//Set member values to 1
@Override
public void run() {
//doing specific stuff here
}
}
And here's where the threads are created in main:
int argSize = Integer.parseInt(args[0]);
w = new int[argSize];
//Initialize w
for (int i = 0; i < argSize; i++) {
wThreads wt = new wThreads(i);
for (int j = 0; j < argSize - 1; j++) {
Thread t = new Thread(wt);
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
So apparently this is not an example of threads working in parallel. They are waiting for each other to finish. I know that the join() and start() calls should be in different loops, but how can I reference the thread in a different loop than the one it's initially created in?
Do I need to create an array of threads in the class declaration? Should the loops be in there as well, and outside of main?
I'm quite confused and any info would be appreciated. Thank you!
forloop.