Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

ความแตกต่างระหว่าง CountDownLatch และ CyclicBarrier ใน Java Concurrency


CountDownLatch และ CyclicBarrier ใช้ในสภาพแวดล้อมแบบมัลติเธรดและทั้งคู่ก็เป็นส่วนหนึ่ง

ตาม Java Doc -

CountDownLatch - ตัวช่วยการซิงโครไนซ์ที่ช่วยให้เธรดหนึ่งหรือหลายเธรดรอจนกว่าชุดของการดำเนินการในเธรดอื่นจะเสร็จสิ้น

CyclicBarrier - ตัวช่วยการซิงโครไนซ์ที่ช่วยให้ชุดของเธรดทั้งหมดรอกันและกันถึงจุดกั้นทั่วไป

ซีเนียร์ เลขที่ คีย์ CyclicBarrier CountDownLatch
1
พื้นฐาน
ตัวช่วยการซิงโครไนซ์ที่ช่วยให้ชุดของเธรดที่รอกันและกันถึงจุดกั้นทั่วไป


ตัวช่วยการซิงโครไนซ์ที่ช่วยให้เธรดหนึ่งหรือหลายเธรดรอจนกว่าชุดของการดำเนินการในเธรดอื่นจะเสร็จสิ้น


2
Ruunable
มีคอนสตรัคเตอร์ที่สามารถจัดเตรียม Runnable ได้
ไม่มีตัวสร้างดังกล่าว
3
กระทู้ /Task
มันรักษาจำนวนเธรด
นับจำนวนงาน
4.
ขั้นสูง
มันไม่ก้าวหน้า
เป็นเรื่องที่ก้าวหน้า
5
ข้อยกเว้น
หากเธรดหนึ่งถูกขัดจังหวะขณะรอ เธรดอื่นที่รออยู่ทั้งหมดจะส่ง B rokenBarrierException
เฉพาะกระทู้ปัจจุบันเท่านั้นที่จะโยน
InterruptedException. จะไม่กระทบกับกระทู้อื่นๆ

ตัวอย่าง CyclicBarrier

public class Main {
   public static void main(String args[]) throws InterruptedException {
      ExecutorService executors = Executors.newFixedThreadPool(4);
      CyclicBarrier cyclicBarrier = new CyclicBarrier(5);
      executors.submit(new Service1(cyclicBarrier));
      executors.submit(new Service1(cyclicBarrier));
      executors.submit(new Service2(cyclicBarrier));
      executors.submit(new Service2(cyclicBarrier));
      executors.submit(new Service2(cyclicBarrier));
      Thread.sleep(3000);
      System.out.println("Done");
   }
}
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class Service1 implements Runnable {
   CyclicBarrier cyclicBarrier;
   public Service1(CyclicBarrier cyclicBarrier) {
      super();
      this.cyclicBarrier = cyclicBarrier;
   }
   @Override
   public void run() {
      System.out.println("Services1" + cyclicBarrier.getNumberWaiting());
      while (true) {
         try {
            cyclicBarrier.await();
         } catch (InterruptedException | BrokenBarrierException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }
   }
}
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class Service2 implements Runnable {
   CyclicBarrier cyclicBarrier;
   public Service2(CyclicBarrier cyclicBarrier) {
      super();
      this.cyclicBarrier = cyclicBarrier;
   }
   @Override
   public void run() {
      System.out.println("Services2" + cyclicBarrier.getNumberWaiting());
      while (true) {
         try {
            cyclicBarrier.await();
         } catch (InterruptedException | BrokenBarrierException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }
   }
}

ตัวอย่าง CountDownLatch

public class Main {
   public static void main(String args[]) throws InterruptedException {
      ExecutorService executors = Executors.newFixedThreadPool(4);
      CountDownLatch latch= new CountDownLatch(2);
      executors.submit(new Service1(latch));
      executors.submit(new Service2(latch));
      latch.await();
      System.out.println("Done");
   }
}
import java.util.concurrent.CountDownLatch;

public class Service1 implements Runnable {

   CountDownLatch latch;
   public Service1(CountDownLatch latch) {
      super();
      this.latch = latch;
   }

   @Override
   public void run() {
      try {
         Thread.sleep(20000);
      } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      latch.countDown();
      System.out.println("Services2"+latch.getCount());
   }
}
import java.util.concurrent.CountDownLatch;
public class Service2 implements Runnable {
   CountDownLatch latch;
   public Service2(CountDownLatch latch) {
      super();
      this.latch = latch;
   }
   @Override
   public void run() {
      try {
         Thread.sleep(20000);
      } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      latch.countDown();
      System.out.println("Services2"+latch.getCount());
   }
}