[concurrency-interest] Traversal and removing elements from ConcurrentskipListSet

Dileep Mandapam dileep.mandapam at gmail.com
Tue May 16 22:36:05 EDT 2017


Thanks for your answers..

I wrote a test program to verify.

import java.util.Comparator;
import java.util.Iterator;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.Executors;

/**
 * Created by dmandapam on 5/16/17.
 */
public class ConcurrentSj {
    static final ConcurrentSkipListSet<MyBug> concurrentSkipListSet = new
ConcurrentSkipListSet<>(new Comparator<MyBug>() {
        @Override
        public int compare(MyBug o1, MyBug o2) {
            return 1;
        }
    });
    public static void main(String... rag) throws InterruptedException {
        Executors.newSingleThreadExecutor().execute(() -> {
            while (true) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                MyBug bug = new MyBug((int) (Math.random() * 10000));
                concurrentSkipListSet.add(bug);
                System.out.println("added bug" + bug);
            }
        });
        while (true) {
            boolean flag = false;
            System.out.println("weak iterators");
            for(MyBug bug : concurrentSkipListSet) {
                if (flag) {
                    boolean remove = concurrentSkipListSet.remove(bug);
                    System.out.println("removed = "+ bug + " result = "+
remove);
                }
                flag = !flag;
            }
            /*Iterator<MyBug> iterator = concurrentSkipListSet.iterator();
            while (iterator.hasNext()) {
                MyBug bug = iterator.next();
                if (flag) {
                    boolean remove = concurrentSkipListSet.remove(bug);
                    System.out.println("removed = "+ bug + " result = "+
remove);
                }
                flag = !flag;
            }*/
            Thread.sleep(1000);
        }
    }

    static class MyBug {
        int id;

        public MyBug(int id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "id=" + id ;
        }

        /*@Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            MyBug bug = (MyBug) o;

            return id == bug.id;
        }

        @Override
        public int hashCode() {
            return id;
        }*/
    }
}


Looks like there is a bug in ConcurrentskipListSet.

On Wed, May 17, 2017 at 6:32 AM, Martin Buchholz <martinrb at google.com>
wrote:

> There's a new API for what you're trying to do - removeIf
> (and probably there's work to be done on our part making it optimal)
>
> On Tue, May 16, 2017 at 5:43 AM, Dileep Mandapam <
> dileep.mandapam at gmail.com> wrote:
>
>> Hi
>>
>> Environment details:-
>> java version "1.8.0_131"
>> Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
>> Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
>>
>>
>>
>>  Removing an element while traversal . Sometimes elements are not
>> removing from concurrentskiplistset.
>>
>> for( Element e : concurrentskiplistsetcollection) {
>>
>>      if ( e is to be removed) {
>>             concurrentskiplistset.remove(e);
>>     }
>>
>> }
>>
>> My question is safe to use above to pattern to remove elements ?
>> --
>> Regards
>> Dileep M
>>
>> _______________________________________________
>> Concurrency-interest mailing list
>> Concurrency-interest at cs.oswego.edu
>> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>>
>>
>


-- 
Regards
Dileep M
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20170517/32c7a805/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ConcurrentSj.java
Type: application/octet-stream
Size: 2393 bytes
Desc: not available
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20170517/32c7a805/attachment.obj>


More information about the Concurrency-interest mailing list