Hello anonymous internet user :) 

I cant understand the full context of the problem and am curious about it. 
I don't understand how aggregating prices into a mean value but  can be 
used to produce a meaningful signal. So some questions:

Do you reset the average periodically?
Do you take snapshots and analyse the discrete time buckets?
Do you just use an average over the whole day?


Why does the code use decimals? I have never measured performance of x87 
but I would expect them to degrade performance. I've always seen money 
represented as int/long types and a fixed scale but its good that we can 
ignore accuracy.

Have you tried sticking everything on a lock free queue from all these 
threads and processed the queue and populated this in a single threaded 
context? I think that fundamentally you can't perform the two operations 
atomically without the hackery Dr Kabutz has propounded :P.

Final thoughts:
a. You care more that the read is fast rather than having the most recent 
data.
b. You don't want to block the writing threads.
c. Thread local averages sound but performing an aggregation is 
incompatible with a.
d. I think you should focus on more than this component of this code.

Thanks in advance if you can answer and of this :)  

p.s none of this is advice, im just another nobody on the internet
On Friday, June 3, 2022 at 11:53:55 PM UTC+1 Dain Ironfoot 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/3a392bc2-cd5c-4c37-91c0-6c1310e80a7fn%40googlegroups.com.

Reply via email to