当前位置:网站首页>Understand in depth why not use system.out.println()
Understand in depth why not use system.out.println()
2022-07-26 18:32:00 【Moon night candle peak】
One 、 Preface
About why not use... In the project System.out.println() The explanation of , There are a lot of information on the Internet, but it is not accurate or detailed , Today, we will make an in-depth analysis of its causes and possible impact on the project .
The atmosphere of this article is three The main points of , The source code parsing 、 The impact analysis 、 Code demonstration .
Two 、 The source code parsing
open println Source code , notice println The method body contains synchronized, That is, the control with lock , In other words, there will be the possibility of blocking , Later we will think of creating congestion to demonstrate .
Here's the picture :

open out Source code , Find out out use final static To modify , That's static variables , It can be considered as a single machine jvm This variable is globally unique in , Here's the picture :
3、 ... and 、 The impact analysis
Through the above two figures , We can think of such a scene , stay System.out.println() In operation , Or for system reasons or other reasons , Leading to the execution of println When , There's a blockage , So the consequence is , All use System.out.println() The place of , Will be in block state , Even lead to system downtime and so on .
as for System.out.println() Its own performance problem , It can be used as a simple performance optimization , It is also not recommended to use , Since it is an in-depth analysis , Let's look at the serious scene first , This is what leads to disabling Syste.out.println() Important reasons .
Four 、 Code emulation
According to the source code , Simulate such a scenario , For some reason ,System.out In the locked state , It hasn't been released for a long time , What will happen .
Simulation steps :
1、 Create two new threads : Thread one and thread two
2、 Let thread one and thread two run for three seconds , Began to System.out Lock in
3、 Check that thread one and thread two are in System.out Printing and thread status when locking
4、 Release System.out lock , Check the operation of thread 1 and thread 2 here
Here is the test code :
package com.zhufeng.thread;
public class ZhufengDemo {
public static Object o = new Object();
public static void main(String[] args) throws InterruptedException {
ZhufengThread t1= new ZhufengThread(" One ");
ZhufengThread t2= new ZhufengThread(" Two ");
Thread thread1 = new Thread(t1);
Thread thread2 = new Thread(t2);
thread1.start();
thread2.start();
System.out.println("3 Seconds later, right System.out Lock ...");
Thread.sleep(3000);
//synchronized ( o){
synchronized ( System.out){
System.out.println(" Locked ...");
for(int i=10;i>0;i--){
System.out.println(" count down :"+i+"—— Thread status :"
+thread1.getState()+", Thread 2 status "+thread2.getState());
Thread.sleep(1000);
}
}
System.out.println(" Lock released ...");
}
}
The operation logic here is carried out according to the steps described just now , Only... Is used in the thread System.out.println Print , stay System.out When locked , See if the two threads will continue printing
package com.zhufeng.thread;
public class ZhufengThread implements Runnable {
private String name;
public ZhufengThread(String name){
this.name=name;
}
@Override
public void run() {
while (true){
System.out.println(name+" Thread running ...");
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Every in the thread 1.5 Print once per second .
We run the print program , Check the console log :

Conclusion :
You can see through the console log , Thread one and thread two are running , If System.out When locked , Two threads are in BLOCKED state , And not printing , wait for 10 second System.out When the lock is released , Start printing again .
So , If it is widely used in production System.out.println(), It will block all logic that uses this statement , The impact is self-evident .
边栏推荐
- Some tips for printing logs
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 3 record
- 凝心聚力,心心向印!印度中资手机企业协会(CMA)正式运营!
- 分布式链路追踪Jaeger在Golang中的使用
- [unity3d] rocker
- J9数字论:如何避免踩雷多头陷阱?
- 突发!Arm停止与华为合作!对华为影响究竟有多大?
- Oracle第二天(视图、索引、plsql、游标、存储过程和存储函数、触发器、jdbc访问存储过程和存储函数)
- The step jumping expansion problem of sword finger offer
- MPLS实验
猜你喜欢

Leetcode 50 day question brushing plan (day 1 - add two numbers 11.00-12.30)

Nailing third-party service provider application ISV application development and listing tutorial

如何组装一个注册中心

更安全、更健康、无续航焦虑,魏牌拿铁DHT-PHEV来了

MPLS实验

云服务器mySQL提示报错

College personnel management system based on jsp+servlet

俄语翻译的就业前景怎样 如何做好俄语翻译工作

链表-倒数最后k个结点

SSM practice day 5
随机推荐
Mpc5744p burning to 98% can not continue to download the program
Vector CANoe Menu Plugin拓展入门
Redis持久化RDB/AOF
Download and configuration of irrklang audio library
PS_ 2_ layer
ssm练习第二天_项目拆分moudle_基本增删改查_批量删除_一对一级联查询
文件上传下载测试点
The second set of 2020 American Asian individual match
LeetCode50天刷题计划(Day 1—— 两数相加 11.00-12.30)
MySQL 遇到过死锁问题吗,你是如何解决的?
链表-合并两个排序的列表
The second day of SSM practice_ Project split moudle_ Basic addition, deletion, modification and query_ Batch delete_ One to one cascading query
Online stock trading, where to choose to open an account is safer?
The faith of circle justice
8.2 some algebraic knowledge (groups, cyclic groups and subgroups)
继续卷技术 埋头苦学,越学越会
Ten year structure five year life-06 impulse to leave
Greedy - 455. Distribute cookies
贪心——455. 分发饼干
《圆圈正义》的信念