[concurrency-interest] Traversal and removing elements from ConcurrentskipListSet

Dileep Mandapam dileep.mandapam at gmail.com
Wed May 17 10:10:53 EDT 2017


Thanks, I rewrote the program. I ran into a strange situation. I am
mutating a field which is used in the comparator.  Now remove method is
returning false for most of the times.


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

/**
 * Created by dmandapam on 5/17/17.
 */
public class RemoveTest {
    static final ConcurrentSkipListSet<MyBug> concurrentSkipListSet =
new ConcurrentSkipListSet<>(new Comparator<MyBug>() {
        @Override
        public int compare(MyBug o1, MyBug o2) {
            int result = Integer.compare(o1.getId(), o2.getId());
            return result;
        }
    });
    public static void main(String... args) {
        MyBug bug1 = new MyBug(10);
        concurrentSkipListSet.add(bug1);
        concurrentSkipListSet.add(new MyBug(20));
        concurrentSkipListSet.add(new MyBug(30));
        bug1.setId(50);
        boolean remove = concurrentSkipListSet.remove(new MyBug(30));
        System.out.println("remove " + remove);

    }
    static class MyBug {
        int id;

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

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

        public void setId(int id) {
            this.id = id;
        }

        public int getId() {
            return id;
        }
    }



*But if i try  with TreeSet then remove method always returns true.
Could you guys help me in uderstanding the problem.*


On Wed, May 17, 2017 at 12:59 PM, Michael Kuhlmann <concurrency at kuli.org>
wrote:

> And in addition to Martin's comments, which already named the issue,
> this is not what I meant with the iterator:
>
> Am 17.05.2017 um 04:36 schrieb Dileep Mandapam:
> >             /*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;
> >             }*/
>
> Of course you should call remove() on the iterator itself, otherwise
> that part of your code would do nothing different than the previous.
>
> Anyway, fix your comparator, and do something with your main() method,
> whatever you're expecting - currently it's stuck in an endless loop,
> totally independent of your list instance and whatever you do with it.
>
> -Michael
> _______________________________________________
> 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/bc8b9390/attachment.html>


More information about the Concurrency-interest mailing list