Re: ConcurrentSkipListMap visibility guarantees
Martin Buchholz via Concurrency-interest <[email protected]> Wed, 3 Jun 2020 19:10:50 -0700
| Newsgroups | gmane.comp.java.jsr.166-concurrency |
|---|---|
| Message-ID | <CA+kOe08-Co+oZB7TL8BJ-xfz6JYfnUoBK0JdPUsL=xyi6SuOTw@mail.gmail.com> |
DummyTimer has no constructor, so they are all created with a sequence of zero, so initially they compare equal, so the map considers them equivalent, and so the initial add may fail. On Wed, Jun 3, 2020 at 6:00 PM Alexandre De Champeaux via Concurrency-interest <[email protected]> wrote: > Hello concurrency interest, > > > > I just came across some old code that concurrently does the following: > > 1. Remove Object o from ConcurrentSkipListMap > 2. Mutate Object o > 3. Add Object o back > > Sometimes, the remove will fail. > > > > I was wondering how badly this is abusing the guarantees of > ConcurrentSkipListMap, and what makes the remove fail? > > > > Here is some code that reproduces (tried with Java 8u241 and 13.0.2): > > > > > > public class ConcurrentSkipListMapMissingRemove { > > private final static AtomicLong SEQUENCE_CREATOR = new AtomicLong(); > > private final static Comparator<DummyTimer> myTimerComparator = > Comparator.comparing(DummyTimer::getSequence); > > private final static ConcurrentSkipListMap<DummyTimer, Long> theTimers > = new ConcurrentSkipListMap<>(myTimerComparator); > > > > public static void main(String[] args) throws InterruptedException { > > for (int i = 0; i < 10; ++i) { > > CompletableFuture.runAsync(() -> { > > DummyTimer myDummyTimer = new DummyTimer(); > > myDummyTimer.start(); > > > > while (true) { > > myDummyTimer.restart(); > > } > > }); > > } > > > > Thread.sleep(100_000_000); > > } > > > > private static class DummyTimer { > > private volatile long theSequence; > > > > void start() { > > add(); > > } > > > > void restart() { > > remove(); > > theSequence = SEQUENCE_CREATOR.getAndIncrement(); > > add(); > > } > > > > private void add() { > > theTimers.put(this, theSequence); > > } > > > > private void remove() { > > if (theTimers.remove(this) == null) { > > System.out.println("Ooops, failed to remove..."); > > } > > } > > > > public long getSequence() { > > return theSequence; > > } > > } > > } > > > > Thanks, > > > > Alex > [image: IMC Logo] <https://www.imc.com/us/> > > [image: F] <https://www.facebook.com/IMCTrading> > > [image: t] <http://twitter.com/IMCTrading> > > [image: I] <https://www.instagram.com/imctrading/> > > [image: in] <https://www.linkedin.com/company/imc-financial-markets> > > *imc.com* <https://www.imc.com/us/> > ------------------------------ > The information in this e-mail is intended only for the person or entity > to which it is addressed. > > It may contain confidential and /or privileged material. If you are not > the intended recipient, please notify us immediately and delete it from > your system. Any other use or disclosure by you, including through > automated tools operating on your systems is prohibited. > _______________________________________________ > Concurrency-interest mailing list > [email protected] > http://cs.oswego.edu/mailman/listinfo/concurrency-interest > _______________________________________________ Concurrency-interest mailing list [email protected] http://cs.oswego.edu/mailman/listinfo/concurrency-interest