System.out.println对多线程的影响

原文网址:System.out.println对多线程的影响_IT利刃出鞘的博客-CSDN博客

简介

本文介绍Java中System.out.println对多线程的影响。

在Java中,我们经常会使用System.out.println打印数据来进行验证,但是它对多线程是有影响的。

多线程的死循环

代码示例

java 复制代码
package com.example.a;

public class Demo {
    private static boolean stopRequest = false;

    public static void main(String[] args) {

        Thread backgroundThread = new Thread(() -> {
            while (!stopRequest) {
                // System.out.println("11");
            }
        });

        backgroundThread.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        stopRequest = true;

    }
}

上边的代码会死循环,因为stopRequested一直是false。

结果如下:

原因分析

一开始主线程中的stopRequest;的变量值为false,创建了一个线程Thread-0,将initFlag复制到运行内存中,因为Thread-0在运行的时候initFlag一直都是false,因为while循环会一直运行,后面的线程Thread-1虽然改变了主内存里面stopRequest为true了,但是影响不了Thread-0运行内存中的stopRequest的值。因此Thread-0会一直在while中无限循环;

println导致跳出死循环

上边是文章的部分内容,为便于维护,全文已转移到此网址:System.out.println对多线程的影响 - 自学精灵