首页 > 代码库 > System.out.println 的多线程并发问题

System.out.println 的多线程并发问题

如果println函数的参数为常量则不会出现线程并发问题,但是如果参数为表达式形式,则JVM在执行println函数的时候会分为几步来执行,从而造成并发问题。

如下例子所示:

package xiaoye.java;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

public class Test
{
	public static void main(String[] args)
	{
		ExecutorService pool = Executors.newFixedThreadPool(2);
		Runnable t1 = new MyRunnable("张三", 2000);
		Runnable t2 = new MyRunnable("李四", 3600);
		Runnable t3 = new MyRunnable("王五", 2700);
		Runnable t4 = new MyRunnable("老张", 600);
		Runnable t5 = new MyRunnable("老牛", 1300);
		Runnable t6 = new MyRunnable("老朱", 800);
		//执行各个线程
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t3);
		pool.execute(t4);
		pool.execute(t5);
		pool.execute(t6);
		//关闭线程池
		pool.shutdown();
	}
}

class MyRunnable implements Runnable
{
	private static AtomicLong aLong = new AtomicLong(10000); //原子量,每个线程都可以自由操作
	private String name; //操作人
	private int data; //操作数

	MyRunnable(String name, int data)
	{
		this.name = name;
		this.data = http://www.mamicode.com/data;>
经过多次执行,其中一次结果如下:
        李四执行了3600,当前余额:15600
        王五执行了2700,当前余额:18300
        老张执行了600,当前余额:18900
        老牛执行了1300,当前余额:20200
        老朱执行了800,当前余额:21000
        张三执行了2000,当前余额:12000

对于 

System.out.println(name + "执行了" + data + ",当前余额:" + aLong.addAndGet(data));


经过反编译后得到如下实际代码:

System.out.println((new StringBuilder(String.valueOf(name))).append("执行了").append(data).append(",当前余额:").append(aLong.addAndGet(data)).toString());

而对于System.out.println() 方法,它的执行代码如下:

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

所以,输出过程需要经过两个步骤,转化字符串和同步输出。

实际上的执行过程是: 张三——李四——王五——老张——老牛——老朱,而实际上输出 张三 却是称为最后一个输出的。这是因为 张三 在或的锁之前被打断了。

如果我们想要输出正确的执行顺序,可以加一个显示锁:

package xiaoye.java;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test {
	public static void main(String[] args) {
		ExecutorService pool = Executors.newFixedThreadPool(2);
		Lock lock = new ReentrantLock(false);
		Runnable t1 = new MyRunnable("张三", 2000, lock);
		Runnable t2 = new MyRunnable("李四", 3600, lock);
		Runnable t3 = new MyRunnable("王五", 2700, lock);
		Runnable t4 = new MyRunnable("老张", 600, lock);
		Runnable t5 = new MyRunnable("老牛", 1300, lock);
		Runnable t6 = new MyRunnable("老朱", 800, lock);
		// 执行各个线程
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t3);
		pool.execute(t4);
		pool.execute(t5);
		pool.execute(t6);
		// 关闭线程池
		pool.shutdown();
	}
}

class MyRunnable implements Runnable {
	private static AtomicLong aLong = new AtomicLong(10000); // 原子量,每个线程都可以自由操作
	private Lock lock;
	private String name; // 操作人
	private int data; // 操作数

	MyRunnable(String name, int data, Lock lock) {
		this.name = name;
		this.data = http://www.mamicode.com/data;>
这样,无论如何运行,程序的输出顺序和操作的执行顺序都保持一致。

System.out.println 的多线程并发问题