zoukankan      html  css  js  c++  java
  • cocurrent包countdownlatch 倒计时门栓

    latch 英[lætʃ]
    美[lætʃ]
    n. 门闩; 弹簧锁;

    锁是每个类的成员变量,它是这个类的固有属性,当然要声明为成员变量。

    成员变量的初始化是通过对象的构造函数的。

    锁是每个类的成员变量,它是这个类的固有属性,当然要声明为成员变量。

    成员变量的初始化是通过对象的构造函数的。

    锁是每个类的成员变量,它是这个类的固有属性,当然要声明为成员变量。

    成员变量的初始化是通过对象的构造函数的。

    12. 闭锁 CountDownLatch

    java.util.concurrent.CountDownLatch 是一个并发构造,它允许一个或多个线程等待一系列指定操作的完成。
    CountDownLatch 以一个给定的数量初始化。countDown() 每被调用一次,这一数量就减一。通过调用 await() 方法之一,线程可以阻塞等待这一数量到达零。

    CountDownLatch 以一个给定的数量初始化。countDown() 每被调用一次,这一数量就减一。通过调用 await() 方法之一,线程可以阻塞等待这一数量到达零。
    以下是一个简单示例。Decrementer 三次调用 countDown() 之后,等待中的 Waiter 才会从 await() 调用中释放出来。

    [java] view plain copy
     
      1. CountDownLatch latch = new CountDownLatch(3);  
      2.   
      3. Waiter      waiter      = new Waiter(latch);  
      4. Decrementer decrementer = new Decrementer(latch);  
      5.   
      6. new Thread(waiter)     .start();  
      7. new Thread(decrementer).start();  
      8.   
      9. Thread.sleep(4000);  
      10.   
      11. public class Waiter implements Runnable{  
      12.   
      13.     CountDownLatch latch = null;  
      14.   
      15.     public Waiter(CountDownLatch latch) {  
      16.         this.latch = latch;  
      17.     }  
      18.   
      19.     public void run() {  
      20.         try {  
      21.             latch.await();  
      22.         } catch (InterruptedException e) {  
      23.             e.printStackTrace();  
      24.         }  
      25.   
      26.         System.out.println("Waiter Released");  
      27.     }  
      28. }  
      29.   
      30. public class Decrementer implements Runnable {  
      31.   
      32.     CountDownLatch latch = null;  
      33.   
      34.     public Decrementer(CountDownLatch latch) {  
      35.         this.latch = latch;  
      36.     }  
      37.   
      38.     public void run() {  
      39.   
      40.         try {  
      41.             Thread.sleep(1000);  
      42.             this.latch.countDown();  
      43.   
      44.             Thread.sleep(1000);  
      45.             this.latch.countDown();  
      46.   
      47.             Thread.sleep(1000);  
      48.             this.latch.countDown();  
      49.         } catch (InterruptedException e) {  
      50.             e.printStackTrace();  
      51.         }  
      52.     }  
      53. }   
  • 相关阅读:
    数据库第三范式的思考
    channel通道例子
    go 测试代码性能实例
    go 新建項目引入gin失敗
    go 创建切片slice的四种方法
    Hibernate查询操作
    shell 分割训练数据
    hadoop streaming 分桶到不同的part
    C语言调用另一个文件的方法
    在springboot中使用jdbcTemplate(3)
  • 原文地址:https://www.cnblogs.com/panxuejun/p/8565978.html
Copyright © 2011-2022 走看看