package org.hocate.tools.log;

import java.io.OutputStream;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

/**
 * 日志输出线程
 * 
 * @author helyho
 *
 */
public class WriteThread implements Runnable {
	private LinkedBlockingDeque<String>	logDeque;
	private OutputStream[]				outputStreams;

	public WriteThread(OutputStream[] outputStreams) {
		this.logDeque = new LinkedBlockingDeque<String>();
		this.outputStreams = outputStreams;
	}

	/**
	 * 增加消息
	 * 
	 * @param string
	 */
	public synchronized void addLogMessage(String string) {
		logDeque.add(string);
	}

	@Override
	public void run() {
		while (true) {
			try {
				String formatedMessage = logDeque.poll(30, TimeUnit.MINUTES);
				if (formatedMessage != null) {
					for (OutputStream outputStream : outputStreams) {
						if (outputStream != null) {
							outputStream.write(formatedMessage.getBytes());
							outputStream.flush();
						}
					}
				} else if (logDeque.size() == 0) {
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

@@ -81,30 +81,29 @@
 current.
-Linked
+Array
 Blocking
 Deque;%0Ai
@@ -94,19 +94,19 @@
 Blocking
-Deq
+Que
 ue;%0Aimpo
@@ -140,16 +140,47 @@
 eUnit;%0A%0A
+import org.hocate.tools.TEnv;%0A%0A
 /**%0A * %E6%97%A5
@@ -270,30 +270,29 @@
 private 
-Linked
+Array
 Blocking
 Deque%3CSt
@@ -275,35 +275,35 @@
 te ArrayBlocking
-Deq
+Que
 ue%3CString%3E%09logDe
@@ -300,19 +300,19 @@
 ing%3E%09log
-Deq
+Que
 ue;%0A%09pri
@@ -412,19 +412,19 @@
 this.log
-Deq
+Que
 ue = new
@@ -428,22 +428,21 @@
 new 
-Linked
+Array
 Blocking
 Dequ
@@ -437,19 +437,19 @@
 Blocking
-Deq
+Que
 ue%3CStrin
@@ -451,16 +451,22 @@
 String%3E(
+100000
 );%0A%09%09thi
@@ -606,19 +606,19 @@
  %7B%0A%09%09log
-Deq
+Que
 ue.add(s
@@ -661,16 +661,37 @@
 run() %7B%0A
+%09%09int loopCount = 0;%0A
 %09%09while 
@@ -708,16 +708,33 @@
 %09%09try %7B%0A
+%09%09%09%09loopCount++;%0A
 %09%09%09%09Stri
@@ -761,19 +761,19 @@
  log
-Deq
+Que
 ue.poll(
 30, 
@@ -772,9 +772,10 @@
 oll(
-3
+50
 0, T
@@ -788,12 +788,17 @@
 t.MI
-NUTE
+LLISECOND
 S);%0A
@@ -1029,24 +1029,83 @@
 %09%09%09%7D
- else if (logDeq
+else if(isTerminate())%7B%0A%09%09%09%09%09break;%0A%09%09%09%09%7Delse if (loopCount%3E=60*2 && logQue
 ue.s
@@ -1136,16 +1136,35 @@
 ;%0A%09%09%09%09%7D%0A
+%09%09%09%09TEnv.sleep(1);%0A
 %09%09%09%7D cat
@@ -1219,10 +1219,265 @@
 %0A%09%09%7D%0A%09%7D%0A
+%09%0A%09/**%0A%09 * %E6%A3%80%E6%9F%A5%E7%BA%BF%E7%A8%8B%E6%98%AF%E5%90%A6%E5%A4%84%E4%BA%8E%E7%BB%93%E6%9D%9F%E7%8A%B6%E6%80%81%0A%09 * @return%0A%09 */%0A%09public boolean isTerminate()%7B%0A%09%09Thread%5B%5D jvmThread = TEnv.getJVMThreads();%0A%09%09for(Thread threadObj : jvmThread)%7B%0A%09%09%09if(threadObj.getName().contains(%22DestroyJavaVM%22))%7B%0A%09%09%09%09return true;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return false;%0A%09%7D%0A%09%0A
 %7D%0A

修复日志线程锁死进程在 main 完成后不推出的问题