[concurrency-interest] Traversal and removing elements from ConcurrentskipListSet

Brian S O'Neill bronee at gmail.com
Wed May 17 10:23:50 EDT 2017


What are you trying to do exactly? Identify how the set fails when the 
behavior it depends is broken? When an entry is added to the set with a 
specific comparator result, the result must not change for as long as 
the entry exists in the set. In general, comparators must not depend on 
mutable state. Your comparator is broken.

On 2017-05-17 07:10 AM, Dileep Mandapam wrote:
> 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.*
> 
> 


More information about the Concurrency-interest mailing list