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多线程调试的相关代码介绍