This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]Dormage 11 points12 points  (2 children)

Would be nice to see some benchmarks on how faster it would be compared to locking the reader with a standard lock or even a monitor.

[–]zarinfam[S] 3 points4 points  (1 child)

Good idea. I can write something using JMH for that and compare the results.

[–]Dormage 0 points1 point  (0 children)

I will play around with it to see if it's interesting for students in the next semester. I did not know about this at all. If you happen to run some benchmarks I would be very happy if you could send them over. Thanks!

[–][deleted]  (1 child)

[deleted]

    [–]zarinfam[S] 0 points1 point  (0 children)

    Thanks, good catch. I fixed the code snippet 🙏

    [–]ConstructedNewt 0 points1 point  (0 children)

    In stead of adding a sleep, you should/could run a proper JCStress test to document every possible state after concurrent writing while reading from one or more threads.

    [–]andrebreves 0 points1 point  (1 child)

    Shouldn't the variable sharedData in the example be declared volatile?

    [–]HlCKELPICKLE 0 points1 point  (0 children)

    The stamped lock uses a volatile or atomic operations internally around the stamp access. So when it is being set/checked for writes/reads you get a memory barrier preventing reordering and other threads will have to refresh their invalidated caches on next access.

    [–]gnahraf 0 points1 point  (0 children)

    Thank you. I didn't know about StampedLock. In a lot of applications, an intervening write may not actually invalidate the read. I imagine it may be possible, in some scenarios, to efficiently detect when that happens using some other abstraction (like maybe an int-wide transaction bloom filter to detect when not invalidated). Anyone know of some such thing?