Java concurrency in practice - Listing 5.11

In JCiP book, list 5.11 , will this code always wait if any of it is Thread tinterrupted (because it startGate.await()might throw InterruptedException), so that the endGate commit will never be released?

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; 
}} 
+3
source share
1 answer

You are not mistaken. The code actually hangs. Keep in mind that many of their code examples are written to make it clear to the reader what a functional piece of code should do. They do not intend for developers to use their code out of the box without their own tests.

For example, someone asked about creating a cache for self-filling. Someone pointed to the Memoizer section in JCiP, where Tim Peierls is:

Memoizer, . , .

MapMaker Memoizer.

http://old.nabble.com/How-to-implement-a-self-populating-memoizer-cache--td30121001.html

+1

All Articles