[concurrency-interest] Traversal and removing elements from ConcurrentskipListSet

Michael Kuhlmann concurrency at kuli.org
Wed May 17 10:22:14 EDT 2017


You mustn't change the internal state of already inserted elements so
that compare() (or hash() in other cases) changes. This is true for all
cases where such a state is used to balance a structure, like HashMap
and TreeMap and such. The internal ordering in this container will be
wrong then.

This is not really an issue with the ConcurrentSkipListSet, or with
concurrency in general. I recommend you to learn about the correct usage
of Java's basic collection classes before you start with this.

Good luck,
Michael


Am 17.05.2017 um 16:10 schrieb Dileep Mandapam:
> 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
> <mailto: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
>     <mailto:Concurrency-interest at cs.oswego.edu>
>     http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>     <http://cs.oswego.edu/mailman/listinfo/concurrency-interest>
> 
> 
> 
> 
> -- 
> Regards
> Dileep M



More information about the Concurrency-interest mailing list