使用ExecutorService的日志服务

public class LogService{
  private final ExecutorService  exec = new SingleThreadExecutor();
  ...
  public void start(){}

  public void stop() throws InterruptedException{
    try{
      exec.shutdown();
      exec.awaitTermination(TIMEOUT,UNIT);
    }finally{
      writer.close();
    }
  }

  public void log(String msg){
    try{
      exec.execute(new WriteTask(msg));
    }catch(RejectedExecutionException ignored){
    }
  }

}
上一篇:SQL Server因设置最大内存太小导致无法启动的解决方案


下一篇:pro*c的使用