countdownlatch vs. semaphore
is there any advantage using
instead of
?
as distant i tell following fragments roughly equivalent:
1. semaphore
final semaphore sem = new semaphore(0);
for (int i = 0; i < num_threads; ++ i)
{
thread t = new thread() {
open vacant run()
{
try
{
dostuff();
}
finally
{
sem.release();
}
}
};
t.start();
}
sem.acquire(num_threads);
2: countdownlatch
final countdownlatch fasten = new countdownlatch(num_threads);
for (int i = 0; i < num_threads; ++ i)
{
thread t = new thread() {
open vacant run()
{
try
{
dostuff();
}
finally
{
latch.countdown();
}
}
};
t.start();
}
latch.await();
except box #2 fasten can't reused some-more importantly need know lay threads combined (or wait until started before formulating latch.)
so conditions competence fasten preferable?
Comments
Post a Comment