Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement AtomicFloat that is not provided by Java JDK

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to achieve the AtomicFloat that Java JDK does not provide", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to achieve the AtomicFloat that Java JDK does not provide"!

We often use AtomicInteger as a counter, as shown below:

List words = Files.readAllLines (Paths.get ("src/main/resources/dic.txt")); AtomicInteger I = new AtomicInteger (); words.parallelStream () .forEach (word-> {/ / get synonyms, antonyms and related words / / for word. LOGGER.info ("Progress:" + total + "/" + i.incrementAndGet () + "from thread:" + Thread.currentThread ());})

In this code, we need to pay attention to two points, one is parallelStream, the other is the variable I.

The use of parallelStream means that code snippets in forEach may be executed concurrently in different threads, so the incrementAndGet method of variable I should be guaranteed to be atomic, otherwise the data of the counter may go wrong.

No problem. Everything is fine, so far so nice.

One day, our requirements are complicated, we need not only + 1 counters, but also to support decimals, such as 2.5 and 3.1, etc. What's the big deal? don't we support decimals when we replace AtomicInteger with AtomicFloat?

Then we went through the JDK class library and couldn't find AtomicFloat. What's going on?

Finally, the secret was discovered in the last part of java.util.concurrent.atomic 's package-summary.html page:

Additionally, classes are provided only for those types that are commonly useful in intended applications. For example, there is no atomic class for representing byte. In those infrequent cases where you would like to do so, you can use an AtomicInteger to hold byte values, and cast appropriately. You can also hold floats using Float.floatToRawIntBits (float) andFloat.intBitsToFloat (int) conversions, and doubles using Double.doubleToRawLongBits (double) andDouble.longBitsToDouble (long) conversions.

Then we can use AtomicInteger as the basis to implement our own AtomicFloat, and the implementation of AtomicDouble and AtomicByte is similar. Let's take a look at the AtomicFloat implemented in the word participle:

Package org.apdplat.word.util;import java.util.concurrent.atomic.AtomicInteger;/** * because Java does not provide AtomicFloat *, we implement a * @ author Yang Shangchuan * / public class AtomicFloat extends Number {private AtomicInteger bits; public AtomicFloat () {this (0f);} public AtomicFloat (float initialValue) {bits = new AtomicInteger (Float.floatToIntBits (initialValue)) } public final float addAndGet (float delta) {float expect; float update; do {expect = get (); update = expect + delta;} while (! this.compareAndSet (expect, update)); return update;} public final float getAndAdd (float delta) {float expect; float update; do {expect = get () Update = expect + delta;} while (! this.compareAndSet (expect, update)); return expect;} public final float getAndDecrement () {return getAndAdd (- 1);} public final float decrementAndGet () {return addAndGet (- 1);} public final float getAndIncrement () {return getAndAdd (1);} public final float incrementAndGet () {return addAndGet (1) } public final float getAndSet (float newValue) {float expect; do {expect = get ();} while (! this.compareAndSet (expect, newValue)); return expect;} public final boolean compareAndSet (float expect, float update) {return bits.compareAndSet (Float.floatToIntBits (expect), Float.floatToIntBits (update)) } public final void set (float newValue) {bits.set (Float.floatToIntBits (newValue));} public final float get () {return Float.intBitsToFloat (bits.get ());} public float floatValue () {return get ();} public double doubleValue () {return (double) floatValue ();} public int intValue () {return (int) get () } public long longValue () {return (long) get ();} public String toString () {return Float.toString (get ());}} so far, I believe you have a deeper understanding of "how to implement the AtomicFloat that Java JDK does not provide". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report