Home  >  Core Java

Example of ConcurrentSkipListSet in Java

By Arvind Rai, March 23, 2019
ConcurrentSkipListSet is used in concurrent threading environment. ConcurrentSkipListSet maintains the behavior same as TreeSet. The elements in ConcurrentSkipListSet can be accessed simultaneously and element's integrity will be maintained. ConcurrentSkipListSet does not affect the performance. All accesses are lock free and we do not need to worry for synchronization because it handles concurrent accesses.
ConcurrentSkipListSetTest.java
package com.concretepage;
import java.util.concurrent.ConcurrentSkipListSet;
public class ConcurrentSkipListSetTest {
    ConcurrentSkipListSet<String> csls= new ConcurrentSkipListSet<String>();
    class AddThread implements Runnable {
        @Override
        public void run() {
            //adds specified element in the set
            csls.add("A");
            csls.add("B");

            // returns the first element
            String s1=csls.first();

            System.out.println(s1);
            //returns the last element.
            String s2=csls.last();

            System.out.println(s2);
        }
    }
    class SubThread implements Runnable {
        @Override
        public void run() {
            // removes the specified element from the set.
           csls.remove("A");
        }
    }
    public static void main(String... args) {
    	ConcurrentSkipListSetTest ob = new ConcurrentSkipListSetTest();
        new Thread(ob.new AddThread()).start();
        new Thread(ob.new SubThread()).start();
    }
}
In the example there are two threads. There is one object of ConcurrentSkipListSet and two threads are doing operation on this thread.
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us