synchronize threads

A

angelochen960

Hi,

I have a code similar to the following, it has a outer loop, for every
iteration it starts 2 threads to execute some code at background, then
proceed to the next iteration, I'd like the code to wait at end of
every iteration until all the threads have ended, any idea how to
achieve this? Thanks.

Angelo


List<String> lst = new ArrayList();
for (final String j : lst) {
for (int i = 0; i < 2; i++) {
new Thread(new Runnable() {
public void run() {
// do something with string j
}
}).start();
}
// proceed to next iteration when all the thraeds have done
}
 
J

Jan Thomä

Hi,

I have a code similar to the following, it has a outer loop, for every
iteration it starts 2 threads to execute some code at background, then
proceed to the next iteration, I'd like the code to wait at end of
every iteration until all the threads have ended, any idea how to
achieve this? Thanks.

Hi,

you can use a monitor object which you give into the Thread object. Then you
call wait() in the outside loop and notify in the inside loop to
co-ordinate activities of the thread. You can use a counter variable to see
how many threads are still running and call wait() again until the counter
is 0.

Hope that gives you an idea :)

Best regards,
Jan
 
G

Gordon Beaton

I have a code similar to the following, it has a outer loop, for
every iteration it starts 2 threads to execute some code at
background, then proceed to the next iteration, I'd like the code to
wait at end of every iteration until all the threads have ended, any
idea how to achieve this? Thanks.

Call join() on each of the threads you need to wait for:

for (int i=0; i<2; i++) {
t = new Thread(...);
t.start();
}

for (int i=0; i < t.length; i++) {
t.join();
}

/gordon

--
 
L

Lew

Gordon said:
I have a code similar to the following, it has a outer loop, for
every iteration it starts 2 threads to execute some code at
background, then proceed to the next iteration, I'd like the code to
wait at end of every iteration until all the threads have ended, any
idea how to achieve this? Thanks.

Call join() on each of the threads you need to wait for:

for (int i=0; i<2; i++) {
t = new Thread(...);
t.start();
}

for (int i=0; i < t.length; i++) {
t.join();
}


But if you're waiting for completion anyway, why the heck spin off threads for
these actions? Just call them synchronously. Otherwise you are just creating
threads to absolutely no effective purpose whatsoever. But hey, why write
clean, compact, maintainable code?
 
G

Gordon Beaton

But if you're waiting for completion anyway, why the heck spin off
threads for these actions? Just call them synchronously. Otherwise
you are just creating threads to absolutely no effective purpose
whatsoever.

If you are spawning *multiple* threads and waiting for them all to
finish (as the OP seems to be), then you are doing the work
concurrently even if the main thread is only waiting.

Granted the main thread could do the work of *one* of the other
threads, but then you've got a special case to deal with.

/gordon

--
 
L

Lew

Gordon said:
If you are spawning *multiple* threads and waiting for them all to
finish (as the OP seems to be), then you are doing the work
concurrently even if the main thread is only waiting.

Of course - really good use case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top