并发编程笔记(2): HashMap与Hashtable线程安全验证

代码:

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class HashMapTest {
	
	public static void main(String[] args) {
		// HashMap
		Map map = new HashMap<>();
		int count = 10;
		
		CyclicBarrier cyclicBarrier = new CyclicBarrier(count);
		HashMapTestClass hashMapTestClass = new HashMapTestClass(map, cyclicBarrier);
		ThreadGroup threadGroup = new ThreadGroup("hashMapTest");
		
		for(int i = 0; i < count; i++) {
			Thread thread = new Thread(threadGroup, hashMapTestClass);
			thread.start();
		}
		
		// 所有线程执行完之后的通知
		
		while(threadGroup.activeCount() != 0) {
		}
		System.out.println("HashMap : " + map.size());
		
		// HashTable 
		Map map2 = new Hashtable<>();
		int count2 = 10;
		CyclicBarrier cyclicBarrier2 = new CyclicBarrier(count2);
		HashMapTestClass hashMapTestClass2 = new HashMapTestClass(map2, cyclicBarrier2);
		ThreadGroup threadGroup2 = new ThreadGroup("hashTableTest");
		
		for(int i = 0; i < count2; i++) {
			Thread thread = new Thread(threadGroup2, hashMapTestClass2);
			thread.start();
		}
		
		// 所有线程执行完之后的通知
		while(threadGroup2.activeCount() != 0) {
		}
		System.out.println("HashTable : " + map2.size());
	}

}

class HashMapTestClass implements Runnable {
	
	Map map;
	CyclicBarrier cyclicBarrier = null;
	AtomicInteger atomicInteger = new AtomicInteger(0);
	
	public HashMapTestClass(Map map, CyclicBarrier cyclicBarrier) {
		// TODO Auto-generated constructor stub
		this.map = map;
		this.cyclicBarrier = cyclicBarrier;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			//countDownLatch.await();
			System.out.println(Thread.currentThread().getName() + " ready at : " + System.currentTimeMillis());
			cyclicBarrier.await();
			System.out.println(Thread.currentThread().getName() + " start at : " + System.currentTimeMillis());
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BrokenBarrierException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i = 0; i < 10; i++) {
			map.put(Thread.currentThread().getName() + i, i);
			
			try {
				TimeUnit.MILLISECONDS.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}

运行结果

Thread-0 ready at : 1535421980826
Thread-2 ready at : 1535421980827
Thread-1 ready at : 1535421980826
Thread-3 ready at : 1535421980828
Thread-4 ready at : 1535421980828
Thread-5 ready at : 1535421980828
Thread-6 ready at : 1535421980829
Thread-7 ready at : 1535421980829
Thread-8 ready at : 1535421980829
Thread-9 ready at : 1535421980830
Thread-9 start at : 1535421980830
Thread-6 start at : 1535421980830
Thread-8 start at : 1535421980830
Thread-2 start at : 1535421980830
Thread-7 start at : 1535421980830
Thread-5 start at : 1535421980830
Thread-4 start at : 1535421980830
Thread-3 start at : 1535421980830
Thread-0 start at : 1535421980830
Thread-1 start at : 1535421980830
HashMap : 92
Thread-10 ready at : 1535421981843
Thread-11 ready at : 1535421981843
Thread-12 ready at : 1535421981843
Thread-13 ready at : 1535421981844
Thread-14 ready at : 1535421981844
Thread-15 ready at : 1535421981844
Thread-16 ready at : 1535421981844
Thread-17 ready at : 1535421981844
Thread-18 ready at : 1535421981844
Thread-19 ready at : 1535421981844
Thread-10 start at : 1535421981845
Thread-12 start at : 1535421981845
Thread-15 start at : 1535421981845
Thread-16 start at : 1535421981845
Thread-17 start at : 1535421981845
Thread-18 start at : 1535421981845
Thread-11 start at : 1535421981845
Thread-14 start at : 1535421981845
Thread-13 start at : 1535421981845
Thread-19 start at : 1535421981845
HashTable : 100

分析

1、分别使用10个线程对HashMap和Hashtable进行put操作。

2、用CyclicBarrier实现线程的同时执行。

3、并发情况下HashMap为什么少put了几个值呢?

你可能感兴趣的:(Java并发编程,笔记)