Corrections to Chapter 5 of Java Network Programming, Threads

p. 121: A period is missing between the third and fourth sentences of the last paragraph. It should read:

Most of the time, however, you'll want to pass the information to other parts of the program. You can store the result of the calculation in a field and provide a getter method to return the value of that field.

p. 133: In the first paragraph, change "Notice the addition of a start() method" to "Notice the addition of the calculateDigest() method to start the thread."

p. 139: In Example 5-10,

public void synchronized sendDigest (byte[] digest) {

should be

public synchronized void sendDigest (byte[] digest) {

That is, swap synchronized and void.

p. 151: All three versions of the join() method including the noargs variant can throw an InterruptedException. Thus the first method signature on the page should be:

public final void join() throws InterruptedException

p. 159: In line 6 of the first paragraph of the Thread Pools section, "for a even a low-" should be "for even a low".

p. 161: If there are directories, already gzipped files, or other incompressible files, the thread is likely not to exit when it should. To fix this, move the call to incrementFilesCompressed() from the fifth line on p. 162 to right after the line that removes a file from the pool on p. 161. Then the run() method should read as follows:

  public void run() {
    
    while (filesCompressed != GZipAllFiles.getNumberOfFilesToBeCompressed()) {
    
      File input = null;
      
      synchronized (pool) {         
        while (pool.isEmpty()) {
          if (filesCompressed == GZipAllFiles.getNumberOfFilesToBeCompressed()) {
            System.out.println("Thread ending");
            return;
          }
          try {
            pool.wait();
          }
          catch (InterruptedException ex) {
          }
        }

        input = (File) pool.remove(pool.size()-1); 
        incrementFilesCompressed();

      }
    
      // don't compress an already compressed file
      if (!input.getName().endsWith(".gz")) {       
        try {
          InputStream in = new FileInputStream(input);
          in = new BufferedInputStream(in);
          
          File output = new File(input.getParent(), input.getName() + ".gz");
          if (!output.exists()) { // Don't overwrite an existing file
            OutputStream out = new FileOutputStream(output);
            out = new GZIPOutputStream(out);
            out = new BufferedOutputStream(out);
            int b;
            while ((b = in.read()) != -1) out.write(b);
            out.flush();
            out.close();
            in.close();
          }
        }
        catch (IOException e) {
          System.err.println(e);
        }
        
      } // end if 
  
    } // end while
    
  } // end run

p. 163: The example is not always adding files at the begining of the queue like it should. In the synchronized block change

pool.add(files[j]);

to

pool.add(0, files[j]);


[ Java Network Programming Corrections | Java Network Programming Home Page | Table of Contents | Examples | Order from Amazon ] ]

Copyright 2000, 2003 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified January 4, 2003