`

Count Down Latch explanation

阅读更多

Very important paragraph on how the CountDownLatch works. CountDownLatch is a flexible latch implementation that can be used in any of these situations;

it allows one or more threads to wait for a set of events to occur. The latch state consists of a counter initialized

to a positive number, representing the number of events to wait for. The countDown method decrements the counter,

indicating that an event has occurred, and the await methods wait for the counter to reach zero, which happens

when all the events have occurred. If the counter is nonzero on entry, await blocks until the counter reaches zero,

the waiting thread is interrupted, or the wait times out.

The following 2 paragraphs are very important on explaining an example how we use CountDownLatch TestHarness illustrates two common uses for latches. TestHarness creates a number of threads

that run a given task concurrently. It uses two latches, a "starting gate" and an "ending gate". The starting gate is

initialized with a count of one; the ending gate is initialized with a count equal to the number of worker threads.

The first thing each worker thread does is wait on the starting gate; this ensures that none of them starts working

until they all are ready to start. The last thing each does is count down on the ending gate; this allows the master

thread to wait efficiently until the last of the worker threads has finished, so it can calculate the elapsed time.

 

Why did we bother with the latches in TestHarness instead of just starting the threads immediately after they are created?

Presumably, we wanted to measure how long it takes to run a task n times concurrently. If we simply created and

started the threads, the threads started earlier would have a "head start" on the later threads, and the degree of

contention would vary over time as the number of active threads increased or decreased. Using a starting gate allows

the master thread to release all the worker threads at once, and the ending gate allows the master thread to wait for the

last thread to finish rather than waiting sequentially for each thread to finish.

1)    The main thread start many threads, and let all of them await the startGate
2)    The main thread countdown the startGate---release them all
3)    The main thread await the endGate
4)    Every thread ends, the thread will countdown the endGate
5)    When all the threads finished, the main thread will be released to the next 
code line—which calculate the long end, calculate the total execution time.
public class TestHarness {
    public long timeTasks(int nThreads, final Runnable task)
            throws InterruptedException {
        final CountDownLatch startGate = new CountDownLatch(1);
        final CountDownLatch endGate = new CountDownLatch(nThreads);
 
        for (int i = 0; i < nThreads; i++) {
            Thread t = new Thread() {
                public void run() {
                    try {
startGate.await();//await() means current thread holds on
                        try {
                            task.run();
                        } finally {
//if countdown() reaches 0, release all threads waiting on this gate
                            endGate.countDown();
                        }
                    } catch (InterruptedException ignored) { }
                }
            };
            t.start();
        }
 
        long start = System.nanoTime();
        startGate.countDown();
        endGate.await();
        long end = System.nanoTime();
        return end-start;
    }
}
分享到:
评论

相关推荐

    latch相关内容讲解

    Latch SPIN_COUNT 参数(在Oracle 7中称为_LATCH_SPIN_COUNT)用于控制进程在等待Latch时的自旋次数。合理设置该参数可以帮助减少上下文切换开销,提高系统性能。根据系统的具体需求调整该参数是非常重要的。 总之...

    Latch up测试标准

    "Latch up测试标准" Latch up测试标准是JEDEC(Joint Electron Device Engineering Council)制定的一个工业标准,旨在确保半导体器件的可靠性和安全性。Latch up是一种电流泄露现象,当半导体器件在高温、高湿或...

    Oracle性能诊断之——Latch free

    - **调整并发控制参数**:根据实际情况调整与并发控制相关的参数,如`_spin_count`,可以优化Latch的获取策略,减少不必要的等待时间。 - **优化SQL语句和应用程序逻辑**:避免不必要的数据锁定和过度的并发访问,...

    oracle Library cache latch 竞争的解决

    SELECT COUNT(*) number_of_waiters FROM v$session_wait w, v$latch l WHERE w.wait_time = 0 -- 当前正在等待的会话 AND w.event = 'latch free' AND w.p2 = l.latch# AND l.name LIKE 'library%'; ``` 通过...

    Latch type&滞回比较器

    ### Latch Type与滞回比较器详解 #### Latch Type结构概述 在高速信号处理领域,比较器的设计至关重要。为了满足更高的速度需求,除了通过增加电路中的工作电流来提升响应速度这一传统方法之外,另一种有效手段是...

    oracle Latch free等待事件

    Oracle数据库中的"Latch Free"等待事件是数据库性能优化的一个重要概念。在深入探讨这个主题之前,我们先要理解什么是Latch和Lock,以及它们之间的区别。 Latch(锁片或轻量级锁)是一种低级别的同步机制,主要用于...

    Latch up 的原理分析

    "Latch up 的原理分析" Latch up 是一种常见的集成电路故障,发生在 CMOS 芯片中,是由于寄生PNP和NPN双极性BJT之间的相互影响而产生的一低阻抗通路。Latch up 的出现可能会使 VDD 和 GND 之间产生大电流,导致芯片...

    学习动态性能表(11)--v$latch$v$latch_children

    【学习动态性能表(11)--v$latch$v$latch_children】主要关注Oracle数据库中的动态性能视图,尤其是关于latch这一关键概念的监控和分析。latch是一种轻量级的锁定机制,用于保护SGA(System Global Area)中的共享...

    Oracle中的Latch和Lock.pdf

    Oracle数据库是一种广泛使用的大型关系型数据库管理系统,它在处理并发事务时采用了一种称为“Latch”和“Lock”的并发控制机制。Latch(闩锁)和Lock(锁定)都是Oracle用来确保数据一致性、防止数据冲突的关键组件...

    一本关于CMOS Latchup的书,内容详细

    ### CMOS Latchup详解 #### 一、引言 《关于CMOS Latchup的书》是由Steven H. Voldman所著的一本专业书籍,该书详细介绍了CMOS技术中的Latchup现象及其相关解决方案。Latchup是集成电路设计与制造过程中一个非常...

    fpga中latch简介

    ### FPGA中Latch简介 #### 一、什么是Latch? 在FPGA设计中,Latch(锁存器)是一种在异步时序电路系统中使用的存储单元,它对输入信号的电平敏感,用于存储信息。通常情况下,一个锁存器能够存储1比特的信息。...

    oracle_latch

    Oracle通过一个隐含参数`_kghdsidx_count`来控制子共享池的数量。增加子共享池的数目可以提高并发性能,但同时也需要相应的内存资源。每个子共享池都有其独立的LRU(Least Recently Used)列表和Shared Pool Latch。...

    Latch和Lock的区别

    在Oracle数据库系统中,了解和区分Latch(锁存器)和Lock(锁)是非常重要的,因为它们都是数据库并发控制的关键机制,确保数据的一致性和完整性。以下是对这两个概念的详细解释: 1. Latch(锁存器) - 目的:...

    芯片latchup原理总结

    《芯片LATCHUP原理详解及防范措施》 LATCHUP,又称闩锁效应,是集成电路设计中一个重要的问题,尤其对于CMOS工艺的芯片来说,它可能导致芯片功能失效甚至损坏。本文将深入探讨LATCHUP的产生原理以及如何有效地防止...

    Latch_Lock_And_Mutex_Contention_Troubleshooting

    标题与描述:“Latch_Lock_And_Mutex_Contention_Troubleshooting” 知识点详述: ### 1. Latch机制概述 Latch是Oracle数据库中用于管理内存结构并发访问的一种低级机制,主要用于保护短暂访问的内存结构,如缓存...

    Laravel开发-laravel-latch

    在Laravel框架中,"Laravel Latch"是一个用于处理会话、认证和授权的工具,它增强了Laravel原生的安全特性。Latch的核心概念是提供了一种更简单的方式来管理和控制用户状态,确保应用程序的数据安全性和用户体验。在...

    electrostatic discharge and latchup

    静电放电(Electrostatic Discharge,简称ESD)与闩锁效应(Latch-Up)是集成电路设计与制造中两个至关重要的问题,它们直接关系到电路的可靠性和使用寿命。以下是对这两个概念及其相互关联的深入探讨。 ### 静电...

    XLinux高可用性服务器集群方案LATCH HA

    LATCH HA解决方案系统结构:两台主机A,B共享一个磁盘阵列,A为工作机,B为备份机。它们之间以一根心跳线来连接,这被称为“心跳检测”,主要通过一条 RS232检测链路来完成。LATCH HA也采用了网络ping检测来验证系统...

    latch Contention

    ### Latch Contention详解 #### 摘要 本文提供了Oracle RDBMS如何利用锁(latches)保护共享内存(SGA)的概述、常见的锁竞争原因及其解决方案,并总结了Quest Software进行的一些研究,该研究表明调整(现在是未...

Global site tag (gtag.js) - Google Analytics