PooledWeblogpublic 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 BufferedReader(new InputStreamReader(in));
this.out = new BufferedWriter(new OutputStreamWriter(out));
this.numberOfThreads = numberOfThreads;
|
Methods Summary |
---|
public int | getNumberOfThreads()
return numberOfThreads;
| public boolean | isFinished()
return this.finished;
| public void | log(java.lang.String entry)
out.write(entry + System.getProperty("line.separator", "\r\n"));
out.flush();
| public static void | main(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 e) {
System.err.println(e);
e.printStackTrace();
}
| public void | processLogFile()
for (int i = 0; i < numberOfThreads; i++) {
Thread t = new LookupThread(entries, this);
t.start();
}
try {
String entry = null;
while ((entry = in.readLine()) != null) {
if (entries.size() > numberOfThreads) {
try {
Thread.sleep((long) (1000.0/numberOfThreads));
}
catch (InterruptedException e) {}
continue;
}
synchronized (entries) {
entries.add(0, entry);
entries.notifyAll();
}
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();
}
|
|