1、写入日志的活动作为生产者,读取日志的活动做为消费者。
2、保证创建新日志消息的各个子任务是原子的。但不希望在消息加入队列时加锁,即将put方法和take方法放在同步之外。
public class LogService{
private finalBlockingQueue
privatefinal LoggerThread loggerThread;
private final PrintWriterwriter;
@GuardBy("this") privateboolean isShutdown;
@GuardBy("this")private intreservations;
public vidstart(){loggerThread.start();}
public viod stop(){
syndhronized (this) (isShutdown=true;)
loggerThread.interrupt();
}
public void log(String msg)throws InterruptedException{
synchronized (this){
if (isShutdown) throws newIllegalStateException(...);++revations;
}
queue.put(msg);
}
private class loggerThreadextends Thread{
public void run(){
try{
while (true){
try{
synchronized (LogService.this){
if (isShutdown&&reservations==0)break;
}
}
}catch (InterruptedException e){}
}
}
finally{
writer.close();
}
}
}
3、封装ExecutorService通过增加链接,把所有权链从应用程序扩展到了服务,再到线程;每个链上的成员管理它的拥有的服务或线程的生命周期
public class Logservice{
privatefinal ExecutorService exec=newSingleThreadExecutor();
...
publicvoid start(){}
publicvoid stop() throws InterruptedException{
try{
exec.shutdown();
exec.awaitTermination(TIMEOUT,UNIT);
}
finally{
writer.close();
}
}
publicvoid log(String msg){
try{
}
catch(RejectedExecutionException ignored) {}
}
}
4、 一个可识别的对象,置于队列中,意味着当你得到它时,停止一切工作。