Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.out.println hazard in Java EE application

When I was starting study Java, I was being told not to do System.out.println in Java EE application. However I don't really know what is the reason of not doing so.

I am fully aware that if we really need to print an important, it should be logged using logging framework.

What I really want to ask here : is there any real hazard that System.out.println make? Does it cause any performance issue?

like image 233
Rudy Avatar asked Dec 06 '11 10:12

Rudy


People also ask

What is out in system out Println in Java?

out. println is a Java statement that prints the argument passed, into the System. out which is generally stdout. System is a Class. out is a Variable.

Can we use system out Println in class?

Since System. out. println() is not a field declaration, it's not allowed.

What does out mean in system out Println and system out Println?

println() is used to print an argument that is passed to it. The statement can be broken into 3 parts which can be understood separately as: System: It is a final class defined in the java. lang package. out: This is an instance of PrintStream type, which is a public and static member field of the System class.


1 Answers

It's indeed a performance issue. If you dig down into the JDK source code for System.out you'll eventually encounter a synchronized block on the output stream.

That means that if you put enough println calls in your source code, the entire code base will effectively run single-threaded, because all threads are waiting for the synchronization lock.

There are some statistics involved here, a single call to println generally isn't going to bring your entire application to a crawl. The more println calls in your code, the more likely two or more threads will have to wait for eachother.

like image 198
Barend Avatar answered Sep 19 '22 13:09

Barend