- 浏览: 120798 次
- 性别:
- 来自: Singapore
文章分类
- 全部博客 (112)
- Tiger Thread (18)
- Perforce (6)
- Spring (5)
- maven (3)
- log4j (3)
- Quartz Scheduler (4)
- unix and linux (12)
- hibernate (3)
- Enum (1)
- Futures and Options (1)
- Market Making (2)
- Java Basic (11)
- Tibco EMS (3)
- F I X message (5)
- equity derivative (2)
- Sybase (3)
- XML (1)
- JUnit (2)
- J A X B 2.0 (1)
- N I O (1)
- windows batch file (1)
- Cruise Control (1)
- util Class (5)
- ant (1)
- JMS (1)
- profiling (0)
- Sql Server (6)
- GXT (2)
- eclipse (1)
- Generics (1)
- Tibco RV (3)
- Autosys (0)
- Message (1)
最新评论
-
houzhe11:
<property name="proxyTa ...
AOP usage -- BeanNameAutoProxyCreator usage
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;
}
}
发表评论
-
javadoc for Cyclic Barrier
2009-04-24 12:48 841java.util.concurrent.CyclicBarr ... -
Delayed interface and Delay Queue
2009-04-22 17:42 992/** * A standard implementati ... -
Count Down Latch example code
2009-04-22 10:38 1084Key point : 1) 1 task is co ... -
3 ways to break dead lock
2009-04-21 17:30 6991) supply special resources. ... -
Blocking Queue Usage
2009-04-20 11:21 7863 implementations: LinkedBlocki ... -
The usage of Lock and Condition
2009-04-18 12:31 1033//: concurrency/waxomatic2/WaxO ... -
Re entrantLock usage
2009-04-15 17:15 1283a thread can be really interru ... -
new interrupt in java5
2009-04-15 12:08 626In Java 5, Thread.interrupt() i ... -
interrupt
2009-04-15 10:57 7551) Each thread has a boolean in ... -
Executor Service Usage
2009-04-14 18:18 853ExecutorService exec = Executor ... -
Thread Local usage
2009-04-14 17:46 754ThreadLocal usage – from Javado ... -
Timer TimerTask usage
2009-04-14 12:03 683Timer typical usage new Tim ... -
wait, notify及线程通讯机制
2009-02-26 22:42 8001) wait(), notify() 方法被调用的时候,只要 ... -
Java Thread programming basical knowledge
2009-02-26 22:40 874yield() : Give a hint to the th ... -
Scheduled Executor Service
2008-07-22 11:27 1054Executor can return Executor, E ... -
Executor usage
2008-07-22 11:04 836Executor is used to arrange thr ... -
Callable Usage
2008-07-22 10:24 876The important thing need to loo ...
相关推荐
Latch free等待事件 Latch和lock的异同
本文章是关于fpga中latch简介。
latch讲解,latch讲解,绝对经典
Latch up测试标准
国外论文,从最基本的角度讲述latch up原理
LATCH HA解决方案系统结构:两台主机A,B共享一个磁盘阵列,A为工作机,B为备份机。它们之间以一根心跳线来连接,这被称为“心跳检测”,主要通过一条 RS232检测链路来完成。LATCH HA也采用了网络ping检测来验证系统...
Advertisement Latch up .rar (11.21 KB, 下载次数: 441 ) 2010-10-25 13:43 上传 点击文件名下载附件 下载积分: 资产 -2 信元, 下载支出 2 信元 分析
Oracle性能诊断之——Latch free
有关oracle当中锁的描述,Latch和Lock的区别,挺不错的。
Oracle中的Latch和Lock.pdf
一般比较器在要求速度较高时除了增大各级工作电流外,还有一种方法就是使用Latch type结构。
- latch是一种Oracle低级别的内存保护机制,用于序列化对于非常短时间的内存结构的并发访问。 - latch是一种简单的内存结构。 - latch的大小通常在100-200B左右(取决于Oracle的版本和Oracle运行的平台)。 - latch...
oracle latch介绍,介绍的非常非常非常非常非常非常非常非常非常非常非常非常非常好
等待事件row cache lock,latch row cache objects处理过程
oracle latch描述以及使用
图文并茂,解释了latch up产生的原因,并详细介绍了防止latch up 产生的方法
在很多地方都能看到,verilog中if与case语句必须完整,即if要加上else,case后要加上default语句,以防止锁存器的发生,接下来就来说说其中原因。
闩锁效应(latch up)是CMOS必须注意的现象,latch解释为回路更合适,大家以后看到latch up就联想到在NMOS与PMOS里面的回路。 为什么它这么重要?因为它会导致整个芯片的失效,所以latch up是QUAL测试的一种,并且与...
Laravel开发-laravel-latch Laravel中的闩锁集成
所有latch参数及等待事件