Searching...
Monday, 23 June 2014

Thread safe TreeSet (ConcurrentSkipListSet) Example

08:51

Example

package com.vinod.concurrency;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
/**
 * Thread safe implementation for TreeSet.
 * @author vinod.kumaran
 *
 */
public class ConcurrentSkipListSetExample {
	public static void main(String args[]) {
		Set<String> treeSet = new ConcurrentSkipListSet<String>();
		treeSet.add("sunday");
		treeSet.add("monday");
		treeSet.add("tuesday");
		Iterator<String> treeSetIterator = treeSet.iterator();
		while (treeSetIterator.hasNext()) {
			String value = treeSetIterator.next();
			treeSet.add("wednesday");
		}
		System.out.println(treeSet);
	}
}

Output



[monday, sunday, tuesday, wednesday]


0 comments: