Java并发编程实战这本书里提到了使用Collections.synchronizedList可以创建线程安全的容器,同时给了一个没有正确使用该容器的反例ListHelper,这个反例看起来实现了同步,然而由于锁不一致导致它并不是一个线程安全的类。代码如下:

class ListHelper <E> {
    public List<E> list = Collections.synchronizedList(new ArrayList<E>());
    public synchronized boolean putIfAbsent(E x) {
        boolean absent = ! list.contains(x);
        if (absent)
            list.add(x);
        return absent;
    }
}

首先我们要知道这个类为什么不是线程安全的?很简单这个类使用了两个不同的锁,putIfAbsent方法上使用的锁是该方法的调用者ListHelper对象,然而list.contains(x)方法使用的却不是这个锁。查看contains的源码,它使用的锁是mutex。

        public boolean contains(Object o) {
            synchronized (mutex) {return c.contains(o);}
        }

显然这个mutex锁是SynchronizedCollection提供的。这就导致了锁不一致的情况,也就会导致线程安全问题。

那么我们如何证明ListHelper是非线程安全的呢?

如下是证明方案,分别启动两个线程,第一个线程循环100000次调用putIfAbsent方法添加数据。第二个线程同样循环100000次添加,但使用了list作为锁对象来进行同步。

public class Test {
	public static void main(String[] args) throws Exception {
		ListHelper<Integer> helper = new ListHelper<>();
		Thread thread1 = new Thread(new Runnable() {
			@Override
			public void run() {
				Test.t1(helper);
			}
		});

		Thread thread2 = new Thread(new Runnable() {
			@Override
			public void run() {
				Test.t2(helper);
			}
		});

		thread1.start();
		thread2.start();
		thread1.join();
		thread2.join();
		System.out.println(helper.list.size());
	}

	private static void t1(ListHelper<Integer> helper) {
		for (int i = 0; i < 100000; i++)
			helper.putIfAbsent(i);
	}

	private static void t2(ListHelper<Integer> helper) {
		for (int i = 0; i < 100000; i++)
			synchronized (helper.list) { // correct way to synchronize
				if (!helper.list.contains(i))
					helper.list.add(i);
			}
	}
}

如果这段测试代码打印的结果大于100000.那么ListHelper就是非线程安全的,如下所示,确实非线程安全。

当然这个案例关注的问题是:单纯来看putIfAbsent 这个方法,它本身一定是线程安全的,但由于该方法使用的锁不正确,导致了putIfAbsent所属的类却不是线程安全的。如果你开启100000个线程交替执行putIfAbsent方法,那么始终输出100000这个结果。

同时案例引发的思考是:如果引用外部线程安全的容器,那么必须保证这个容器和类方法使用同一个锁,这个类才是线程安全的类。

所以,ListHelper要改造成线程安全的类,必须使用和list一致的锁,即使用如下的同步代码块的方式:

class ListHelper <E> {
    public List<E> list = Collections.synchronizedList(new ArrayList<E>());
    public boolean putIfAbsent(E x) {
        synchronized (list) {
            boolean absent = ! list.contains(x);
            if (absent)
                list.add(x);
            return absent;
        }
    }
}

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐