What are the gains in actual computing speed (verbosity of code being put aside) and for which case would each be recommended for, as a good design pattern? I would like to know the above in general but also if it helps in the context of the following which can re-written in all 3 ways
static void parallelTestsExecution(String name,Runnable ... runnables){
ArrayList<BaseTestThread> list = new ArrayList();
int counter =0; // use local variable counter to name threads accordingly
for(Runnable runnable : runnables){//runnable contains test logic
counter++;
BaseTestThread t = new BaseTestThread(runnable);
t.setName(name+" #"+counter);
list.add(t);
}
for(BaseTestThread thread : list ){//must first start all threads
thread.start();
}
for(BaseTestThread thread : list ){//start joins only after all have started
try {
thread.join();
} catch (InterruptedException ex) {
throw new RuntimeException("Interrupted - This should normally not happen.");
}
}
for(BaseTestThread thread : list ){
assertTrue("Thread: "+thread.getName() , thread.isSuccessfull());
}
}
vs
static void parallelTestsExecution(String name,Runnable ... runnables){
ArrayList<BaseTestThread> list = new ArrayList();
int counter =0; // use local variable counter to name threads accordingly
for(Runnable runnable : runnables){
counter++;
BaseTestThread t = new BaseTestThread(runnable);//BaseTestThread extends thread
t.setName(name+" #"+counter);
list.add(t);
}
list.forEach((thread) -> {
thread.start();
});
list.forEach((thread) -> {
try {
thread.join();
} catch (InterruptedException ex) {
throw new RuntimeException("Interrupted - This should normally not happen.");
}
});
list.forEach((thread) -> {
assertTrue("Thread: "+thread.getName() , thread.isSuccessfull());
});
}
vs Populating the list with threads each associated with a runnable and using list.Stream()... etc (currently not confident enough with this one to post something more concrete)
I am specifically interested in learning how each of those different mechanisms work and what are their strength/weaknesses to apply my better judgment whenever its needed to decide what to use as a best practice. Question isnt about micro-optimisation in general and the importance of it.
list
tothreads
. It's also a shame that there isn't an easy way to map an array of element to an array of elements and indices (That would allow you to really easily map those runnables to threads)