JAVA IO - 重定向输出

public class RedirectTest {
    public static void main(String args[]){
    	System.out.println("hello normal");
    	System.setOut(System.err);
    	System.out.println("hello error");
    	//System.out.println("hello error1");
    }
}

对于以上代码有时候输出

hello normal
hello error

有时

hello error
hello normal

原因

I believe this is because you are writing to two different outputs (one is standard out and the other standard error). These are probably handled by two different threads at runtime to allow writing to both during java execution. Assuming this is the case, the cpu task scheduler is not going to execute the threads in the same order every time.

You should never get this functionality if all of your output is going to the same output stream (ie everything goes to standard out or everything goes to standard err). You will never be guaranteed execution order of standard error vs standard output.

No need to talk about two different threads... the key is that standard output and error are distinct streams here :-) 

http://stackoverflow.com/questions/12594537/random-printing-order-for-system-out-system-err-calls



转载于:https://my.oschina.net/u/138995/blog/191848

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值