Thanks Heinz! Please correct me if I am wrong but your approach, on a high level, sounds similar to Remi's approach? In Remi's approach, we are CASing and atomically updating the state ourselves. However, in your approach, we would delegate it to LongAccumulator?
On Saturday, June 4, 2022 at 5:11:07 AM UTC-5 Heinz Kabutz wrote: > What is the maximum count and sum? Could the two fit into a 64 bit long? > If so a LongAccumulator could be made to work. > > On Sat, 04 Jun 2022 at 01:53, Dain Ironfoot <[email protected]> wrote: > >> Hello, >> >> I am writing a class to calculate average of prices. We use it to >> generate buy/sell signal for some financial instruments therefore latency >> is crucial. Prices are sent via different threads therefore class needs to >> be thread-safe. >> >> Am I correct in understanding that I have to use a lock to make the two >> operations (adding and incrementing) atomic? I have looked at >> AtomicLong/LongAdder/LongAccumulator but looks like they can only sum the >> numbers atomically. >> >> *In other words, there is no way to do this in a lock-free manner? * >> >> Thank you! >> >> >> >> public final class Computer{ >> >> private ReentrantReadWriteLock rwLock; >> private ReadLock readLock; >> private WriteLock writeLock; >> >> private BigDecimal sum; >> private int count; >> >> public AverageCalculatorThreadSafeImplementation2( ){ >> this.rwLock = new ReentrantReadWriteLock(); >> this.readLock = rwLock.readLock(); >> this.writeLock = rwLock.writeLock(); >> this.sum = new BigDecimal(0); >> } >> >> public final void add( double value ){ >> writeLock.lock(); >> try{ >> sum = sum.add( BigDecimal.valueOf(value) ); >> ++count; >> }finally{ >> writeLock.unlock(); >> } >> } >> >> public final double compute( ){ >> readLock.lock(); >> try{ >> return sum.divide(BigDecimal.valueOf(count)).doubleValue(); >> }finally{ >> readLock.unlock(); >> } >> } >> >> -- >> You received this message because you are subscribed to the Google Groups >> "mechanical-sympathy" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> > To view this discussion on the web, visit >> https://groups.google.com/d/msgid/mechanical-sympathy/2d083587-2ee1-4f8f-a7ee-a99062a6ef5cn%40googlegroups.com >> >> <https://groups.google.com/d/msgid/mechanical-sympathy/2d083587-2ee1-4f8f-a7ee-a99062a6ef5cn%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- > Dr Heinz M. Kabutz (PhD CompSci) > Author of "The Java(tm) Specialists' Newsletter" > Sun/Oracle Java Champion > JavaOne Rockstar Speaker > http://www.javaspecialists.eu > Tel: +30 69 75 595 262 > Skype: kabutz > -- You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web, visit https://groups.google.com/d/msgid/mechanical-sympathy/72a2e165-c119-4dbb-a561-444539fb540dn%40googlegroups.com.
