集合的线程安全问题

ArrayList的线程安全问题

代码

package com.mine.juc.collection;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ArrayListDemo {

	public static void main(String[] args) {
		final List list = new ArrayList();

		for (int i = 1; i <= 100; i++) {
			new Thread(new Runnable() {
				public void run() {
					list.add(UUID.randomUUID().toString().substring(0, 10));
					System.out.println(list);
				}
			}).start();
		}
	}

}

问题

Exception in thread "Thread-21" [e0ea16e2-d, 19f4d8ec-b, ef4f030d-c, 0224861c-c, e7683cba-3, b3a2f7b9-0, 8eb49864-4, c8664e88-e, 31b5e3ef-0, 51560422-a, 5436893c-a, 31b11696-8]Exception in thread "Thread-29" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
	at java.util.ArrayList$Itr.next(ArrayList.java:851)
	at java.util.AbstractCollection.toString(AbstractCollection.java:461)

解决方案:

  • Vector
  • Collections
  • CopyOnWriteArrayList

代码
CopyOnWriteArrayList(Vector同)

package com.mine.juc.collection;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

public class CopyOnWriteArrayListDemo {

	public static void main(String[] args) {
		final List list = new CopyOnWriteArrayList();

		for (int i = 1; i <= 100; i++) {
			new Thread(new Runnable() {
				public void run() {
					list.add(UUID.randomUUID().toString().substring(0, 10));
					System.out.println(list);
				}
			}).start();
		}
	}

}

Collections

package com.mine.juc.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

public class CollectionsDemo {

	public static void main(String[] args) {
		final List list = new ArrayList();
		final List synchronizedList = Collections.synchronizedList(list);

		for (int i = 1; i <= 100; i++) {
			new Thread(new Runnable() {
				public void run() {
					synchronizedList.add(UUID.randomUUID().toString().substring(0, 10));
					System.out.println(synchronizedList);
				}
			}).start();
		}
	}

}

HashSet的线程安全问题

HashSet数据结构是一个HashMap,把值放入key,所以HashSet是不能重复的。
代码

package com.mine.juc.collection;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class HashSetDemo {

	public static void main(String[] args) {
		final Set set = new HashSet();
		
		for (int i = 1; i <= 100; i++) {
			new Thread(new Runnable() {
				public void run() {
					set.add(UUID.randomUUID().toString().substring(0, 10));
					System.out.println(set);
				}
			}).start();
		}
	}

}

问题

Exception in thread "Thread-82" java.util.ConcurrentModificationException
	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
	at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
	at java.util.AbstractCollection.toString(AbstractCollection.java:461)
	at java.lang.String.valueOf(String.java:2994)
	at java.io.PrintStream.println(PrintStream.java:821)
	at com.mine.juc.collection.HashSetDemo$1.run(HashSetDemo.java:16)
	at java.lang.Thread.run(Thread.java:745)
Exception in thread "Thread-86" java.util.ConcurrentModificationException
	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
	at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
	at java.util.AbstractCollection.toString(AbstractCollection.java:461)
	at java.lang.String.valueOf(String.java:2994)
	at java.io.PrintStream.println(PrintStream.java:821)
	at com.mine.juc.collection.HashSetDemo$1.run(HashSetDemo.java:16)
	at java.lang.Thread.run(Thread.java:745)

解决方案:

  • CopyOnWriteArraySet
  • Collections(略)

代码

package com.mine.juc.collection;

import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

public class CopyOnWriteArraySetDemo {

	public static void main(String[] args) {
		final Set set = new CopyOnWriteArraySet();
		
		for (int i = 1; i <= 100; i++) {
			new Thread(new Runnable() {
				public void run() {
					set.add(UUID.randomUUID().toString().substring(0, 10));
					System.out.println(set);
				}
			}).start();
		}
	}

}

HashMap的线程安全问题

代码

package com.mine.juc.collection;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class HashMapDemo {

	public static void main(String[] args) {
		final Map map = new HashMap();
		
		for (int i = 1; i <= 100; i++) {
			final String key = String.valueOf(i);
			new Thread(new Runnable() {
				public void run() {
					map.put(key, UUID.randomUUID().toString().substring(0, 10));
					System.out.println(map);
				}
			}).start();
		}
	}

}
Exception in thread "Thread-85" java.util.ConcurrentModificationException
	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
	at java.util.HashMap$EntryIterator.next(HashMap.java:1463)
	at java.util.HashMap$EntryIterator.next(HashMap.java:1461)
	at java.util.AbstractMap.toString(AbstractMap.java:531)
	at java.lang.String.valueOf(String.java:2994)
	at java.io.PrintStream.println(PrintStream.java:821)
	at com.mine.juc.collection.HashMapDemo$1.run(HashMapDemo.java:17)
	at java.lang.Thread.run(Thread.java:745)
Exception in thread "Thread-97" java.util.ConcurrentModificationException
	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
	at java.util.HashMap$EntryIterator.next(HashMap.java:1463)
	at java.util.HashMap$EntryIterator.next(HashMap.java:1461)
	at java.util.AbstractMap.toString(AbstractMap.java:531)
	at java.lang.String.valueOf(String.java:2994)
	at java.io.PrintStream.println(PrintStream.java:821)
	at com.mine.juc.collection.HashMapDemo$1.run(HashMapDemo.java:17)
	at java.lang.Thread.run(Thread.java:745

解决方案:

  • ConcurrentHashMap
  • Collections(略)

代码

package com.mine.juc.collection;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashMapDemo {

	public static void main(String[] args) {
		final Map map = new ConcurrentHashMap();
		
		for (int i = 1; i <= 100; i++) {
			final String key = String.valueOf(i);
			new Thread(new Runnable() {
				public void run() {
					map.put(key, UUID.randomUUID().toString().substring(0, 10));
					System.out.println(map);
				}
			}).start();
		}
	}

}

你可能感兴趣的:(java,java,哈希算法,散列表)