一个基于生产者消费者原理的日志系统
2010-03-13 04:40
357 查看
简单的框架,功能未实现
/**
* 这个类是个日志系统的简单框架,使用阻塞队列blockingqueue来工作
* 该类的记录由一个内部类来实现,该内部类作为一个现场来运行,它从blockingqueue中取出信息来,将其写入到writer中
* 工作过程类似于生产者,消费者原理
* @author 王法波
* @version 1.0
*
*/
public class LoggerService {
/**
* 阻塞队列
*/
private final BlockingQueue<String> queue;
/**
* 内部类对象
*/
private final LoggerThread loggerThread;
/**
* writer对象
*/
private final PrintWriter writer;
/**
* 判定是否关闭
*/
private boolean isShutdown;
/**
* 行数
*/
private int reservations;
/**
* 初始队列长度
*/
private final int SIZE = 100;
/**
* 构造函数
* @throws FileNotFoundException
*/
public LoggerService() throws FileNotFoundException {
queue = new ArrayBlockingQueue<String>(SIZE);
loggerThread = new LoggerThread();
writer = new PrintWriter(new File(""));
}
/**
* 启动消费者线程
*/
public void start() {
loggerThread.start();
}
/**
* 停止线程
*/
public void stop() {
synchronized (this) {
isShutdown = true;
}
loggerThread.interrupt();
}
/**
* 向日志中写入信息
* @param msg
* @throws InterruptedException
*/
public void log(String msg) throws InterruptedException {
synchronized (this) {
if(isShutdown)
throw new IllegalStateException("");
++reservations;
}
queue.put(msg);
}
/**
* 内部类
* @author Administrator
*
*/
private class LoggerThread extends Thread {
public void run() {
while (true) {
synchronized (LoggerService.this) {
if (isShutdown && reservations == 0)
break;
String msg;
try {
msg = queue.take();
synchronized (LoggerService.this) {
--reservations;
}
writer.println(msg);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
writer.close();
}
}
}
}
}
}
/**
* 这个类是个日志系统的简单框架,使用阻塞队列blockingqueue来工作
* 该类的记录由一个内部类来实现,该内部类作为一个现场来运行,它从blockingqueue中取出信息来,将其写入到writer中
* 工作过程类似于生产者,消费者原理
* @author 王法波
* @version 1.0
*
*/
public class LoggerService {
/**
* 阻塞队列
*/
private final BlockingQueue<String> queue;
/**
* 内部类对象
*/
private final LoggerThread loggerThread;
/**
* writer对象
*/
private final PrintWriter writer;
/**
* 判定是否关闭
*/
private boolean isShutdown;
/**
* 行数
*/
private int reservations;
/**
* 初始队列长度
*/
private final int SIZE = 100;
/**
* 构造函数
* @throws FileNotFoundException
*/
public LoggerService() throws FileNotFoundException {
queue = new ArrayBlockingQueue<String>(SIZE);
loggerThread = new LoggerThread();
writer = new PrintWriter(new File(""));
}
/**
* 启动消费者线程
*/
public void start() {
loggerThread.start();
}
/**
* 停止线程
*/
public void stop() {
synchronized (this) {
isShutdown = true;
}
loggerThread.interrupt();
}
/**
* 向日志中写入信息
* @param msg
* @throws InterruptedException
*/
public void log(String msg) throws InterruptedException {
synchronized (this) {
if(isShutdown)
throw new IllegalStateException("");
++reservations;
}
queue.put(msg);
}
/**
* 内部类
* @author Administrator
*
*/
private class LoggerThread extends Thread {
public void run() {
while (true) {
synchronized (LoggerService.this) {
if (isShutdown && reservations == 0)
break;
String msg;
try {
msg = queue.take();
synchronized (LoggerService.this) {
--reservations;
}
writer.println(msg);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
writer.close();
}
}
}
}
}
}
相关文章推荐
- C# 系统日志处理-生产者与消费者模式
- 基于C#中的Trace实现一个简单的日志系统
- mysql dba系统学习(10)innodb引擎的redo log日志的原理 mysql dba系统学习(11)管理innodb引擎的redo log日志的一个问题
- 一个基于observer模式的游戏事件分发系统
- 一个日志输出系统的设计
- 《一个著名的日志系统是怎么设计出来的?》
- 《一个著名的日志系统是怎么设计出来的?》
- 浅谈生产者消费者模型(Linux系统下的两种实现方法)
- 基于Flume的美团日志收集系统(一)架构和设计
- 基于C#分步式聊天系统的在线视频直播系统 之 FY.Logfiles(日志文件组件)
- 一起谈.NET技术,发布NGuestBook(一个基于.NET平台的分层架构留言本小系统)
- Orchard一个基于ASP.NET MVC下的CMS系统。
- 优客社区是一个基于 Spring Boot 的轻量级社区系统
- 基于VC++6.0系统设计一个小型的信息管理应用程序。如航空订票系统、学生管理系统、图书管理系统,学生自选的项目等。
- 一个基于组合模式的游戏地图系统
- 日志系统之基于flume收集docker容器日志
- 基于Flume的美团日志收集系统(二)改进和优化
- Spring Cloud 5分钟搭建教程(附上一个分布式日志系统项目作为参考)
- 一个失败的生产者,消费者代码
- 终于找到了一个基于ASP.NET MVC框架下的CMS系统——Orchard!