使用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){
    
    
    }
  }

}

猜你喜欢

转载自blog.csdn.net/weixin_37632716/article/details/118583111
今日推荐