Java多線程調試是很繁瑣的但是還是需要我們不斷進行相關的學習下面我們就來看看在Java多線程調試中需要我們注意的相關關鍵點為了查看中間結果一般采用將信息輸出到Systemout中
這樣就產生了一個問題很多個線程的輸出信息都簡單的輸出到控制台上當我們要查看每個線程自己處理的中間信息時很麻煩最近在一個實踐項目中使用了Logj讓每個線程輸出到自己的log文件中這樣在調試的時候很方便
配置logjproperties文件
lgjrootLogger=DEBUGstdout
# stdout is set to be a ConsoleAppender
logjappenderstdout=orgapachelogjConsoleAppender
logjappenderstdoutlayout=orgapachelogj
PatternLayout
logjappenderstdoutlayoutConversionPattern=
[%t] (%F%L) %m%n
logjadditivitylogjstdout=false
# comsecrawlerCrawler Appender
seCrawlerCrawler=DEBUGCrawler
logjappenderCrawler=orgapachelogjFileAppender
logjappenderCrawlerFile=Crawlerlog
logjappenderCrawlerAppend=false
logjappenderCrawlerlayout=orgapachelogjPatternLayout
logjappenderCrawlerlayoutConversionPattern=%m%n
secrawlerCrawler=false
# comsecrawlerLinkProcessor Appender
secrawlerLinkProcessor=DEBUGLink
Processor
logjappenderLinkProcessor=orgapachelogjFileAppender
logjappenderLinkProcessorFile=LinkProcessorlog
logjappenderLinkProcessorAppend=false
logjappenderLinkProcessorlayout=orgapachelogj
PatternLayout
logjappenderLinkProcessorlayoutConversionPattern=%m%n
secrawlerLinkProcessor=false
說明rootLogger為控制台CrawlerLinkProcessor為兩個線程分別輸出到Crawlerlog和LinkProcessorlog文件中
在線程中添加日志代碼
……
import orgapachelogjLogger;
……
public class Crawler extends Thread {
……
protected static Logger logger = Logger
getLogger(Crawlerclass);
……
loggerdebug(msg);
……
}
這樣在線程運行的時候會自動產生兩個日志文件Crawlerlog和LinkProcessorlog並將中間結果輸出到各自的日志文件中以上就是對Java多線程調試的相關代碼介紹
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27439.html