📄 pooledweblog.java
字号:
//PooledWeblog.java
import java.io.*;
import java.util.*;
import com.macfaq.io.SafeBufferedReader;
public class PooledWeblog {
private BufferedReader in;
private BufferedWriter out;
private int numberOfThreads;
private List entries = Collections.synchronizedList(new
LinkedList( ));
private boolean finished = false;
private int test = 0;
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;
}
public boolean isFinished( ) {
//当结束时返回
return this.finished;
}
public int getNumberOfThreads( ) {
return numberOfThreads;
}
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( );
} // 循环终止
}
catch (IOException e) {
System.out.println("Exception: " + e);
}
this.finished = true;
// 结束那些仍然等待的线程
synchronized (entries) {
entries.notifyAll( );
}
}
public void log(String entry) throws IOException {
out.write(entry + System.getProperty("line.separator", "\r\n"));
out.flush( );
}
public static void main(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( );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -