我正在学习《Java并发编程实践》这本书,并遇到了CountDownLatch。
以下是给出的示例:
这是它的解释:
TestHarness在5.11清单中展示了锁存器(latch)的两种常见用途。TestHarness创建了多个线程并发运行给定的任务。它使用了两个锁存器,一个“起始闸门”和一个“结束闸门”。起始闸门的计数初始化为1;结束闸门的计数初始化为工作线程数。每个工作线程做的第一件事情是等待起始闸门;这确保了在所有线程都准备好开始之前,它们都不会开始工作。它们做的最后一件事情是在结束闸门上倒数;这样主线程就可以高效地等待,直到所有工作线程都完成,以便它可以计算流逝的时间。
我对Java多线程还不熟悉,所以我无法理解给出的解释以及这个程序是如何工作的。
这句话的意思是什么:
每个工作线程做的第一件事情是等待起始闸门;这确保了在所有线程都准备好开始之前,它们都不会开始工作。它们做的最后一件事情是在结束闸门上倒数;这样主线程就可以高效地等待,直到所有工作线程都完成,以便它可以计算流逝的时间。
这段代码的工作原理是什么:
``` public static 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(); try { task.run(); } finally { endGate.countDown(); } } catch (InterruptedException ignored) { } } }; t.start(); } long start = System.nanoTime(); startGate.countDown(); endGate.await(); long end = System.nanoTime(); return end - start; } ```
请帮助我理解这个概念。
以下是给出的示例:
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();
try {
task.run();
} finally {
endGate.countDown();
}
} catch (InterruptedException ignored) { }
}
};
t.start();
}
long start = System.nanoTime();
startGate.countDown();
endGate.await();
long end = System.nanoTime();
return end-start;
}
}
这是它的解释:
TestHarness在5.11清单中展示了锁存器(latch)的两种常见用途。TestHarness创建了多个线程并发运行给定的任务。它使用了两个锁存器,一个“起始闸门”和一个“结束闸门”。起始闸门的计数初始化为1;结束闸门的计数初始化为工作线程数。每个工作线程做的第一件事情是等待起始闸门;这确保了在所有线程都准备好开始之前,它们都不会开始工作。它们做的最后一件事情是在结束闸门上倒数;这样主线程就可以高效地等待,直到所有工作线程都完成,以便它可以计算流逝的时间。
我对Java多线程还不熟悉,所以我无法理解给出的解释以及这个程序是如何工作的。
这句话的意思是什么:
每个工作线程做的第一件事情是等待起始闸门;这确保了在所有线程都准备好开始之前,它们都不会开始工作。它们做的最后一件事情是在结束闸门上倒数;这样主线程就可以高效地等待,直到所有工作线程都完成,以便它可以计算流逝的时间。
这段代码的工作原理是什么:
``` public static 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(); try { task.run(); } finally { endGate.countDown(); } } catch (InterruptedException ignored) { } } }; t.start(); } long start = System.nanoTime(); startGate.countDown(); endGate.await(); long end = System.nanoTime(); return end - start; } ```
Thread t = new Thread() {
public void run() {
try {
startGate.await();
try {
task.run();
} finally {
endGate.countDown();
}
} catch (InterruptedException ignored) { }
}
};
and
long start = System.nanoTime();
startGate.countDown();
endGate.await();
long end = System.nanoTime();
return end-start;
请帮助我理解这个概念。
CountDownLatch
开始呢?要慢下来,从这里开始:http://docs.oracle.com/javase/tutorial/essential/concurrency/。请注意,我已经翻译了整段话并且没有添加任何额外的信息。 - Sotirios Delimanolis