Any system that logs vast amounts of information, needs to think about performance. The activity of logging cannot be a synchronous blocking call that returns only when the message has been logged to a persistence store. Enterprise logging systems usually
make use of a message bus to carry messages asynchronously to their target persistence store. Be it a database or a file.
Talking about logging brings us to System.out.println() (Lets call is SOP for short). It is a surprisingly commonly method to “log” messages. SOP is not meant to be used as a logging system, but unfortunately there is no dearth of projects that have these
statements scattered around the code base. The adverse effects that this statement can bring on the performance of the system is often not recognized as well as it should be.
Why is SOP a bottleneck for performance ? This is why…
Code excerpt from PrintStream.java:
private void write(String s)
{
try
{
synchronized (this)
{
ensureOpen();
textOut.write(s);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush && (s.indexOf('\n') >= 0))
out.flush();
}
}
catch (InterruptedIOException x)
{
Thread.currentThread().interrupt();
}
catch (IOException x)
{
trouble = true;
}
}
All SOP calls on a String result in a corresponding call to the private write(String s) method in PrintStream.java. The synchronized block ensures that every thread has to wait for this call to end before it can proceed. Whats more, the calls to flushBuffer()
on the BufferedWriter that is textOut result in the execution of more synchronized blocks. These blocks are expensive to execute.
Here is a chart that shows how performance degrades when a program logs 100,000 messages through various threads. The rate of degradation also depends on the number of characters that are passing through the stream.
System.out.println performance:
The degradation experienced in ‘Web application X / Y’ may vary, but it cannot be discounted. Avoid using SOPs to log messages in your app. Even one or two that are left over can harm performance under the right conditions. Let us also not forget about e.printStackTrace()
and other forms of writing to console output that follow the same synchronized pattern.
分享到:
相关推荐
统一管理日志打印与通过System.out.println();打印
package day2;... System.out.println("请您输入你要倒排的数据和这个数据的位数 : "); Scanner sca=new Scanner(System.in); long nu=sca.nextInt(); long mm=sca.nextInt(); niXuShuChu(nu,mm);
主要介绍了 java 中 System.out.println()和System.out.write()的区别.的相关资料,需要的朋友可以参考下
当我们看到# System.out.println()这行代码的时候,我相信很多人都会回答,这有什么好聊的?不就是一条打印语句吗? 那我问你一句,你如何理解System.out.println() 我相信大多数朋友都回答不出来他到底是个什么...
主要介绍了idea输入sout无法自动补全System.out.println()的问题,本文给大家分享解决方案,供大家参考,需要的朋友可以参考下
Java循环的一个小问题,java中for(int i= 0; i;i=i++)System.out.print(i);为什么无限输出0
System.out.println("where 显示当前在什么地方") System.out.println("list 列出当前位置的物品") System.out.println("list allspots 列出大厦内所有地点") System.out.println("list spots 列出当前位置...
System.out.println(3/2); System.out.println(3d/2); System.out.println("------------------------"); System.out.println("s"+"b"); System.out.println('s'+'b');//int+int System.out.println('s'+...
System.out.println(" 中国移动 "); System.out.println(" 1.查看 "); System.out.println(" 2.添加 "); System.out.println(" 3.修改 "); System.out.println(" 4.删除 "); System.out.println(" 5....
下面是一个用Java编写的消防知识... System.out.println("1. 火灾发生时,应该先采取哪些措施?"); System.out.println("A. 拨打119报警电话"); System.out.println("B. 救人"); System.out.println("C. 扑灭火源");
case SerialPortEvent.BI:System.out.println("BI");break; case SerialPortEvent.CD:System.out.println("CD");break; case SerialPortEvent.CTS:System.out.println("CTS");break; case SerialPortEvent.DSR:...
System.out.println("您输入的身份证号不是18位或不是全数字!")
System.out.println(" 1. RS-Hash Function Value: " + ghl.RSHash(key)); System.out.println(" 2. JS-Hash Function Value: " + ghl.JSHash(key)); System.out.println(" 3. PJW-Hash Function Value: " + ghl....
//加法 System.out.println(b1.add(b2)); //减法 System.out.println(b2.subtract(b1)); //乘法 System.out.println(b1.multiply(b2)); //除法 System.out.println(b2.divide(b1)); //BigDecimal ⾼精度⼩数运算 ...
注释语句、javadoc、System.out.print和println和printf区别java特殊语句注释语句print、printf、println区别 java特殊语句 转换符 类型 d 十进制 x 十六进制 o 八进制 f 定点浮点数 e 指数浮点数 ...
System.out.println("运行该程序时,请输入以下格式的命令:"); System.out.println("java FileTest <文件名>"); return; } File file = new File(args[0]); System.out.println("文件或目录是否存在:" + ...
System.out.println(""); System.out.println("****************以下输出参考中所有货物的信息***************"); System.out.println(""); System.out.println("goodsTotalNum="+goodsTotalNum); for(int i=0;...
System.out.println("执行"+i); if(i==5){ break;//终止整个for循环. } } //continue的使用 for(int i=1;i;i++){ if(i==5||i==7){ continue;//终止的是循环体中某一次循环。 } ...
System.out.println(s); String s1="abc"; String s2=new String("abc"); System.out.println(s==s1);//false System.out.println(s==s2);//false System.out.println(s1==s2);//false System.out.println...
System.out.println("请选择功能: 1.显示全部记录 2.查询登录记录 0.退出"); int command = scan.nextInt(); switch(command){ case 1: System.out.println("显示全部记录"); break; case 2: ...