System.out.println()对共享变量和多线程的影响,为什么会造成while循环终止

最近在了解volatile关键字,众所周知volatile可以保证共享变量的可见性,本文是记录在学习volatile过程中遇到的有趣事件。
首先看下面的代码:

	private static boolean isStop = false;
    public static void main(String[] args) {
        new Thread() {
            public void run() {
                while (!isStop) {
                }
            };
        }.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        isStop = true;
    }

由于isStop变量并未用volatile修饰,所以这个程序并不会在1s后退出,而是进入死循环,原因我就不再叙述了,百度一下volatile关键字就可以得到解释。你以为这就结束了吗,不,接下来,我在while中加入了一个System.out.println(“hello”);语句:

				while (!isStop) {
                    System.out.println("hello");
                }

这个时候奇怪的事发生了,程序在运行一段时间后退出了,这是为什么呢,第一时间当然是查看源码:

public void println(String x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }

可以看到,println()方法是一个同步方法,锁条件是this,进入到System类中,在110行可以看到维护了一个final static的成员变量PrintStream out:

public final static PrintStream out = null;

初始化在1155行的initializeSystemClass()方法:

 FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
 FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
 FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
 setIn0(new BufferedInputStream(fdIn));
 setOut0(newPrintStream(fdOut, props.getProperty("sun.stdout.encoding")));
 setErr0(newPrintStream(fdErr, props.getProperty("sun.stderr.encoding")));

由于System类中维护了一个final static的成员变量PrintStream out,所以系统中任何地方调用System.out.println()都是同步的,所以在生产环境中不推荐甚至是禁止使用的,可以使用日志来记录需要的信息。

但是这并不能解决我的疑惑,因为synchronized关键字同步,只能同步同步代码块里面的内容,很明显isStop变量是在同步代码块外面的,怎么会同步呢?

百度以后找到了原因,原因是这样的:JVM 会尽力保证内存的可见性,即便这个变量没有被同步关键字修饰。也就是说,只要 CPU 有时间,JVM 会尽力去保证变量值的更新。这种与 volatile 关键字的不同在于,volatile 关键字会强制的保证线程的可见性。而不加这个关键字,JVM 也会尽力去保证可见性,但是如果 CPU 一直有其他的事情在处理,就不能保证变量的更新。第一段代码使用了while死循环,占用了CPU的大量时间,第二段代码在while死循环中增加了System.out.println(),由于是同步的,在IO过程中,CPU空闲时间比较多就有可能有时间去保证内存的可见性。

下面的代码可能会更好的说明问题:

private static boolean isStop = false;
    public static void main(String[] args) {
        new Thread() {
            public void run() {
                while (!isStop) {
                    // System.out.println("hello");
                    try {
                        Thread.sleep(500);
                    } catch (

                    InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
        }.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        isStop = true;
    }

使用Thread.sleep(500);模拟System.out.println()同步输出时CPU的空闲时间,可以发现程序也可以正常退出,并不会一直死循环,这是由于此时CPU有时间去保证内存的可见性。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
// 文件名:moreServer.java import java.io.*; import java.net.*; import java.util.*; /** * <p>Title: 多线程服务器</p> * <p>Description: 本实例使用多线程实现多服务功能。</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Filename: </p> * @author 杜江 * @version 1.0 */ class moreServer { public static void main (String [] args) throws IOException { System.out.println ("Server starting...\n"); //使用8000端口提供服务 ServerSocket server = new ServerSocket (8000); while (true) { //阻塞,直到有客户连接 Socket sk = server.accept (); System.out.println ("Accepting Connection...\n"); //启动服务线程 new ServerThread (sk).start (); } } } //使用线程,为多个客户端服务 class ServerThread extends Thread { private Socket sk; ServerThread (Socket sk) { this.sk = sk; } //线程运行实体 public void run () { BufferedReader in = null; PrintWriter out = null; try{ InputStreamReader isr; isr = new InputStreamReader (sk.getInputStream ()); in = new BufferedReader (isr); out = new PrintWriter ( new BufferedWriter( new OutputStreamWriter( sk.getOutputStream ())), true); while(true){ //接收来自客户端的请求,根据不同的命令返回不同的信息。 String cmd = in.readLine (); System.out.println(cmd); if (cmd == null) break; cmd = cmd.toUpperCase (); if (cmd.startsWith ("BYE")){ out.println ("BYE"); break; }else{ out.println ("你好,我是服务器!"); } } }catch (IOException e) { System.out.println (e.toString ()); } finally { System.out.println ("Closing Connection...\n"); //最后释放资源 try{ if (in != null) in.close (); if (out != null) out.close (); if (sk != null) sk.close (); } catch (IOException e) { System.out.println("close err"+e); } } } }
这段代码是一个Java语言实现的简单聊天室客户端程序,实现了与服务器的通信和信息的发送、接收。以下是代码注释: ```java package 第十一讲_网络编程; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.SocketException; public class ChatClient extends Thread{ Socket sc;// 对象sc,用来处理与服务器的通信 BufferedReader in;// 声明输入流缓冲区,用于存储服务器发来的信息 PrintWriter out;// 声明打印输出流,用于信息的发送 TcpClientUI ui; public ChatClient(String ip, int port, TcpClientUI ui) {// 初始化ChatClient类 this.ui = ui; try { // 创建sc, 用服务器ip和端口作参数 sc = new Socket(ip, port); System.out.println("已顺利联接到服务器。"); //创建输入输出流对象 in = new BufferedReader(new InputStreamReader(sc.getInputStream())); out = new PrintWriter(sc.getOutputStream(), true); } catch (Exception e) { System.out.println(e); } start();//启动线程 } public void run() { // 用于监听服务器端发送来的信息 String msg = ""; while (true) { try { msg=in.readLine();// 从缓冲区读入一行字符存于msg } catch (SocketException ex) { System.out.println(ex); break; } catch (Exception ex) { System.out.println(ex); } if (msg != null && msg.trim() != "") {// 若msg信息不为空 System.out.println(">>" + msg); ui.mainArea.append(msg + "\n");// 把msg信息添加到客户端的文本区域内 } } } public void sendMsg(String msg) {// 用于发送信息 try { out.println("【客户端】" + msg); } catch (Exception e) { System.out.println(e); } } } ``` 其中,ChatClient类继承自Thread类,实现了多线程的功能,在启动时创建一个Socket对象,与指定的服务器IP和端口进行连接。在连接成功后,创建输入输出流对象,用于接收和发送信息。run()方法中,通过循环不断监听服务器端发送的信息,如果有信息则显示在客户端的文本区域内。sendMsg()方法用于发送信息给服务器端。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值