有多种方式可以在多线程时将整个控制台输出保存到文件中吗?我正在处理5个线程。我有这个想法,我可以在运行方法中放置printstream。使用多线程控制台输出到文件
例如:这里
public void run() {
try{
PrintStream out = new PrintStream(file);
stopExecute stop = new stopExecute();
Thread t = new Thread(stop);
Scanner in = new Scanner(System.in);
t.start();
while (!in.hasNextLine())
{
classThatUsingMultipleThrads();
System.out.println("Finished");
anotherClassThatUsingThreads();
System.out.println("Finished");
}
System.out.prinln("User stopped the execution");
stop.keepRunning = false;
System.setOut(out);
}
catch(IOException e){System.out.println(e);}
问题是,它的唯一可取“用户采空执行”在while循环,一切都不会被保存输出。或者来自其他类的输出流。
我试图把
System.setOut(out);
在while循环
,但并没有帮助。
编辑:拼写校正
“Finished”,而不是“Finnished”:)哦。并“停止”而不是“停止”。 –
会使用像Log4J这样的日志框架来帮助你的情况吗? –
@ C-Otto感谢您的纠正,我每天都在学习新事物:) – Micropop