atomic-long

AtomicLong operations

℡╲_俬逩灬. 提交于 2019-12-07 03:14:52
问题 I need to perform the following operation: // average, total, elapsed are Long's average = ( ( total * average ) + elapsed ) / (++total); But I want to use AtomicLong This is what I'm trying but I don't quite get if it is correct: average.set( (( total.get() * average.get() ) + elapsed) / total.getAndIncrement() ); How can I tell if this is correct? 回答1: Presumably you are using AtomicLong because these numbers are being accessed concurrently. Since you have two numbers involved and you are

AtomicLong operations

浪尽此生 提交于 2019-12-05 07:51:54
I need to perform the following operation: // average, total, elapsed are Long's average = ( ( total * average ) + elapsed ) / (++total); But I want to use AtomicLong This is what I'm trying but I don't quite get if it is correct: average.set( (( total.get() * average.get() ) + elapsed) / total.getAndIncrement() ); How can I tell if this is correct? Presumably you are using AtomicLong because these numbers are being accessed concurrently. Since you have two numbers involved and you are using both a get and incrementAndGet in the same statement, I don't think AtomicLong is the right choice. I

CAS vs synchronized performance

别来无恙 提交于 2019-12-03 17:01:55
问题 I've had this question for quite a while now, trying to read lots of resources and understanding what is going on - but I've still failed to get a good understanding of why things are the way they are. Simply put I'm trying to test how a CAS would perform vs synchronized in contended and not environments. I've put up this JMH test: @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS) @Measurement(iterations = 5,

What is AtomicLong in Java used for?

丶灬走出姿态 提交于 2019-12-03 10:34:36
问题 Can some one explain what AtomicLong is used for? For example, what's the difference in the below statements? private Long transactionId; private AtomicLong transactionId; 回答1: There are significant differences between these two objects, although the net result is the same, they are definitely very different and used under very different circumstances. You use a basic Long object when: You need the wrapper class You are working with a collection You only want to deal with objects and not