Multi-threaded communication: how good are atomic variables like AtomicInteger? Why not AtomicFloat?

Introduction:

I want to create a multi-threaded Android application. My problem is the connection between threads. I read about the relationship between threads, and I came across things like the Looper / Handler design, which seemed pretty attractive and Atomic Variables like AtomicInteger. At the moment, I used AtomicInteger as a message, but since I am not very good at Java, I am not sure if this is bad in my case / if there is a better solution for my specific purpose. I was also a little suspicious of my method when I noticed that I really needed something like AtomicFloat, but it does not exist. I felt that I was losing sight of this concept. I also found that you can make AtomicFloat, but I'm just not sure if I am on the right track or there is a better technique.

Question: Is it good / useful to use Atomic Variables and implement AtomicFloat for my specific purpose (described below), or is there a better way to handle the message?

Purpose / architecture of the application using AtomicVariables:

I have 4 Themes with the following purpose:

1.SensorThread: reads sensor data and stores the most recent values ​​in AtomicVariables, for example

AtomicFloat gyro_z,AtomicFloat gyro_y, ...

2.CommunicationThread: communication with the PC, interprets the commands that enter the socket and sets the state of the application from the point of view of AtomicInteger: AtomicInteger state;

3.UIThread: displays the current sensor values ​​from AtomicFloat gyro_z, AtomicFloat gyro_y,

4.ComputThread: uses sensor values AtomicFloat gyro_z,AtomicFloat gyro_y, ...and status AtomicInteger stateto perform calculations and send commands via USB.

+5
2

-, ( ) . , AtomicInteger AtomicFloat .

, , ReadWriteLock, , :

:.

private ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); //the reentrant impl

....

public void readMethod() {

    readWriteLock.readLock().lock();

    try {
        //code that simply _reads_ your object
    } finally {
        readWriteLock.readLock().unlock();
    }

}

public void writeMethod() {

     readWriteLock.writeLock().lock();

     try {
        //... code that modifies your shared object / objects
     } finally {
         readWriteLock.writeLock().unlock();
     }
}

" " " " .

, , , :

public class SensorRead {

    public java.util.Date dateTimeForSample;

    public float value;

}

, . AtomicXXX .

+1

, AtomicFloat. , volatile float, - compareAndSet addAndGet ( , ).

, , AtomicInteger :

public final int addAndGet(int delta) {
    for (;;) { 
       int current = get();
       int next = current + delta;
       if (compareAndSet(current, next))
           return next;
    }
}

, compareAndSet , , float, , , Float.floatToIntBits int, CAS AtomicInteger, - :

private volatile float value;

public final boolean compareAndSet(float expect, float next) {
    AtomicInteger local = new AtomicInteger();
    for(;;) {
        local.set(Float.floatToIntBits(value)); 
        if(local.compareAndSet(Float.floatToIntBits(expect), 
                               Float.floatToIntBits(next)) {
            set(Float.intBitsToFloat(local.get()));
            return true;
        }
    } 
}

public final float addAndGet(float delta) {
    for (;;) { 
       float current = get();
       float next = current + delta;
       if (compareAndSet(current, next))
           return next;
    }
}
0

All Articles