日期:2014-05-20 浏览次数:20783 次
public class ClientThread extends Thread {
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ClientThread("AAAAA").start();
new ClientThread("BBBBB").start();
new ClientThread("CCCCC").start();
}
}
@Override
public void run(){
System.out.println(getName() + " BEGIN ");
for(int i=0;i<10;i++) {
Log.println("i= " + i);
}
Log.close();
System.out.println(getName() + " END ");
}
}
import java.io.PrintWriter;
public class TSLog {
private static PrintWriter mPrintWriter = null;
public TSLog(String fileName){
try {
mPrintWriter = new PrintWriter(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void println(String msg){
mPrintWriter.write(msg+"\r\n");
}
public static void close(){
System.out.println("END of log");
mPrintWriter.close();
}
}
import java.io.PrintWriter;
public class Log {
private PrintWriter mPrintWriter = null;
private static final ThreadLocal mThreadLocal = new ThreadLocal();
public Log(String fileName){
}
public static void println(String msg){
getLog().println(msg);
}
public static void close(){
getLog().close();
}
private static TSLog getLog(){
TSLog log = (TSLog)mThreadLocal.get();
if(log == null){
log = new TSLog(Thread.currentThread().getName() + "_log.txt");
mThreadLocal.set(log);
}
return log;
}
}
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ClientThread("AAAAA").start();
new ClientThread("BBBBB").start();
new ClientThread("CCCCC").start();
}
}