FileDocCategorySizeDatePackage
PooledWeblog.javaAPI DocExample2313Tue Feb 22 12:53:54 GMT 2005None

PooledWeblog

public class PooledWeblog extends Object

Fields Summary
private BufferedReader
in
private BufferedWriter
out
private int
numberOfThreads
private List
entries
private boolean
finished
private int
test
Constructors Summary
public PooledWeblog(InputStream in, OutputStream out, int numberOfThreads)



         
    this.in = new SafeBufferedReader(new InputStreamReader(in));
    this.out = new BufferedWriter(new OutputStreamWriter(out));
    this.numberOfThreads = numberOfThreads;
  
Methods Summary
public intgetNumberOfThreads()

    return numberOfThreads; 
  
public booleanisFinished()

    return this.finished; 
  
public voidlog(java.lang.String entry)

    out.write(entry + System.getProperty("line.separator", "\r\n"));
    out.flush();
  
public static voidmain(java.lang.String[] args)


    try {
      PooledWeblog tw = new PooledWeblog(new FileInputStream(args[0]), 
       System.out, 100);
      tw.processLogFile();
    }
    catch (FileNotFoundException e) {
      System.err.println("Usage: java PooledWeblog logfile_name");
    }
    catch (ArrayIndexOutOfBoundsException e) {
      System.err.println("Usage: java PooledWeblog logfile_name");
    }
    catch (Exception ex) {
      System.err.println(ex);
      ex.printStackTrace();
    }

  
public voidprocessLogFile()

  
    for (int i = 0; i < numberOfThreads; i++) {
      Thread t = new LookupThread(entries, this);
      t.start();
    }
    
    try {
      String entry = in.readLine();
      while (entry != null) {
        
        if (entries.size() > numberOfThreads) {
          try {
            Thread.sleep((long) (1000.0/numberOfThreads));
          }
          catch (InterruptedException e) {}
          continue;
        }

        synchronized (entries) {
          entries.add(0, entry);
          entries.notifyAll(); 
        }
        
        entry = in.readLine();
        Thread.yield();
        
      } // end while
      
    }
    catch (IOException e) {
      System.out.println("Exception: " + e);
    }
    
    this.finished = true;
    
    // finish any threads that are still waiting
    synchronized (entries) {
      entries.notifyAll(); 
    }