How to complete the processing of information output in Java multithreading debugging
How to complete the information output processing in Java multithreading debugging, aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Java multithreading debugging is very tedious, but we still need to continue to learn about it. Let's take a look at the key points we need to pay attention to in Java multithreaded debugging. In order to see the intermediate results, it is common to output the information to System.out.
This creates a problem: the output information of many threads is simply output to the console, which is troublesome when we want to see the intermediate information processed by each thread itself. Recently, in a practical project, Log4j was used to output each thread to its own log file, which is convenient for debugging.
1. Configure the log4j.properties file
Lg4j.rootLogger=DEBUG,stdout
# stdout is set to be a ConsoleAppender.
Log4j.appender.stdout=org.apache.log4j.ConsoleAppender
Log4j.appender.stdout.layout=org.apache.log4j.
PatternLayout
Log4j.appender.stdout.layout.ConversionPattern=
[% t] (% FJO% L) -% m% n
Log4j.additivity.log4j.stdout=false
# com.se.crawler.Crawler Appender.
Log4j.logger.com.se.Crawler.Crawler=DEBUG,Crawler
Log4j.appender.Crawler=org.apache.log4j.FileAppender
Log4j.appender.Crawler.File=Crawler.log
Log4j.appender.Crawler.Append=false
Log4j.appender.Crawler.layout=org.apache.log4j.PatternLayout
Log4j.appender.Crawler.layout.ConversionPattern=%m%n
Log4j.additivity.com.se.crawler.Crawler=false
# com.se.crawler.LinkProcessor Appender.
Log4j.logger.com.se.crawler.LinkProcessor=DEBUG,Link
Processor
Log4j.appender.LinkProcessor=org.apache.log4j.FileAppender
Log4j.appender.LinkProcessor.File=LinkProcessor.log
Log4j.appender.LinkProcessor.Append=false
Log4j.appender.LinkProcessor.layout=org.apache.log4j.
PatternLayout
Log4j.appender.LinkProcessor.layout.ConversionPattern=%m%n
Log4j.additivity.com.se.crawler.LinkProcessor=false
Description: rootLogger is the console and Crawler,LinkProcessor is two threads, which are output to Crawler.log and LinkProcessor.log files respectively.
two。 Add log code to a thread
……
Import org.apache.log4j.Logger
……
Public class Crawler extends Thread {
……
Protected static Logger logger = Logger.
GetLogger (Crawler.class)
……
Logger.debug (msg)
……
}
In this way, two log files Crawler.log and LinkProcessor.log are automatically generated while the thread is running, and the intermediate results are output to their respective log files.
This is the answer to the question about how to complete the information output processing in Java multithreaded debugging. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.