Saturday, August 22, 2015

实现JDK没有提供的AtomicFloat - 杨尚川的博客 - ITeye技术网站



实现JDK没有提供的AtomicFloat - 杨尚川的博客 - ITeye技术网站
最后终于在java.util.concurrent.atomic的package-summary.html页面的最后部分发现了秘密:
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.
接下来我们就可以利用AtomicInteger作为基础来实现自己的AtomicFloat了,实现AtomicDouble和AtomicByte也是类似的做法,下面看看在word分词中实现的AtomicFloat
  1. import java.util.concurrent.atomic.AtomicInteger;  
  2.   
  3. /** 
  4.  * 因为Java没有提供AtomicFloat 
  5.  * 所以自己实现一个 
  6.  * @author 杨尚川 
  7.  */  
  8. public class AtomicFloat extends Number {  
  9.   
  10.     private AtomicInteger bits;  
  11.   
  12.     public AtomicFloat() {  
  13.         this(0f);  
  14.     }  
  15.   
  16.     public AtomicFloat(float initialValue) {  
  17.         bits = new AtomicInteger(Float.floatToIntBits(initialValue));  
  18.     }  
  19.   
  20.     public final float addAndGet(float delta){  
  21.         float expect;  
  22.         float update;  
  23.         do {  
  24.             expect = get();  
  25.             update = expect + delta;  
  26.         } while(!this.compareAndSet(expect, update));  
  27.   
  28.         return update;  
  29.     }  
  30.   
  31.     public final float getAndAdd(float delta){  
  32.         float expect;  
  33.         float update;  
  34.         do {  
  35.             expect = get();  
  36.             update = expect + delta;  
  37.         } while(!this.compareAndSet(expect, update));  
  38.   
  39.         return expect;  
  40.     }  
  41.   
  42.     public final float getAndDecrement(){  
  43.         return getAndAdd(-1);  
  44.     }  
  45.   
  46.     public final float decrementAndGet(){  
  47.         return addAndGet(-1);  
  48.     }  
  49.   
  50.     public final float getAndIncrement(){  
  51.         return getAndAdd(1);  
  52.     }  
  53.   
  54.     public final float incrementAndGet(){  
  55.         return addAndGet(1);  
  56.     }  
  57.   
  58.     public final float getAndSet(float newValue) {  
  59.         float expect;  
  60.         do {  
  61.             expect = get();  
  62.         } while(!this.compareAndSet(expect, newValue));  
  63.   
  64.         return expect;  
  65.     }  
  66.   
  67.     public final boolean compareAndSet(float expect, float update) {  
  68.         return bits.compareAndSet(Float.floatToIntBits(expect), Float.floatToIntBits(update));  
  69.     }  
  70.   
  71.     public final void set(float newValue) {  
  72.         bits.set(Float.floatToIntBits(newValue));  
  73.     }  
  74.   
  75.     public final float get() {  
  76.         return Float.intBitsToFloat(bits.get());  
  77.     }  
  78.   
  79.     public float floatValue() {  
  80.         return get();  
  81.     }  
  82.   
  83.     public double doubleValue() {  
  84.         return (double) floatValue();  
  85.     }  
  86.   
  87.     public int intValue() {  
  88.         return (int) get();  
  89.     }  
  90.   
  91.     public long longValue() {  
  92.         return (long) get();  
  93.     }  
  94.   
  95.     public String toString() {  
  96.         return Float.toString(get());  
  97.     }  
  98. }  
类似的情况也发生在了FloatStream身上,我们在JDK类库中也找不到FloatStream,看下面这段描述:
There are primitive-specialized versions of Stream for ints, longs, and doubles: IntStream, LongStream, DoubleStream. There are not primitive versions for the rest of the primitive types because it would have required an unacceptable amount of bloat in the JDK. IntStream, LongStream, and DoubleStream were deemed useful enough to include, and streams of other numeric primitives can represented using these three via widening primitive conversion.

Read full article from 实现JDK没有提供的AtomicFloat - 杨尚川的博客 - ITeye技术网站

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts