S
stixwix
Hi,
If I have a program which starts 100 threads in main:
for (int i = 0; i < 100; i++){
String name = Integer.toString(i);
Thread temp = new Thread(new Worker());
temp.setName(name);
temp.start();
}
where the Worker's run method just prints out the threads name, then in
this configuration I see about the last 50 threads print their message.
I initially thought that this is because the thread object referenced
by temp goes out of scope each time round the loop and so could be
garbage collected. But perhaps the fact that they are all in the same
thread group keeps them in scope?
If I use a CyclicBarrier and wait in main after the for loop (and force
each thread to wait at the end of its run method) then all threads are
seen to run.
I know this is a bit of a contrived example but am just interested
whether the thread objects are subject to GC the same as any other if
they can't be seen by main.
Thanks,
Andy
If I have a program which starts 100 threads in main:
for (int i = 0; i < 100; i++){
String name = Integer.toString(i);
Thread temp = new Thread(new Worker());
temp.setName(name);
temp.start();
}
where the Worker's run method just prints out the threads name, then in
this configuration I see about the last 50 threads print their message.
I initially thought that this is because the thread object referenced
by temp goes out of scope each time round the loop and so could be
garbage collected. But perhaps the fact that they are all in the same
thread group keeps them in scope?
If I use a CyclicBarrier and wait in main after the for loop (and force
each thread to wait at the end of its run method) then all threads are
seen to run.
I know this is a bit of a contrived example but am just interested
whether the thread objects are subject to GC the same as any other if
they can't be seen by main.
Thanks,
Andy