相关疑难解决方法(0)

CountDownLatch与Semaphore

使用有什么好处

java.util.concurrent.CountdownLatch

代替

java.util.concurrent.Semaphore?

据我所知,以下片段几乎相同:

1.信号量

final Semaphore sem = new Semaphore(0);
for (int i = 0; i < num_threads; ++ i)
{
  Thread t = new Thread() {
    public void run()
    {
      try
      {
        doStuff();
      }
      finally
      {
        sem.release();
      }
    }
  };
  t.start();
}

sem.acquire(num_threads);
Run Code Online (Sandbox Code Playgroud)

2:CountDownLatch

final CountDownLatch latch = new CountDownLatch(num_threads);
for (int i = 0; i < num_threads; ++ i)
{
  Thread t = new Thread() {
    public void run()
    {
      try
      {
        doStuff();
      }
      finally …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading semaphore countdownlatch

89
推荐指数
4
解决办法
5万
查看次数