I have one doubt relative to the concourrency between UI thread and other threads.
Ui main thread updates the value of different variables : - float - long - boolean
And I have another thread that reads the same variable and does some logic operations (without editing its values) with that and sends local broadcast messages with the result of this operation.
Are there concurrency problems???doI have to use synchronized methods and atomic variables or it doesn't matter ?
I reflect on this problem, because there isn't atomic variable for float primitive , and because I'm scared to block Ui thread with the wrong code..
Then the int,double primitive type are atomic right ? The problem is on long and double.
For example :
class test
{
int c=0;
long p=0;
new Thread1( new Runnable(){
public void run(){
a=a+c;
p=p+c;
}
}
).start();
new Thread2( new Runnable(){
public void run(){
c=function();
p=functionx();
}
}
).start();
....
}
I have one doubt relative concurrent between UI thread and other thread.
Ui main thread update the value of different variable : - float - long - boolean
And I have another thread that reads the same variable and does some logic operation (without edit its values) with that and send local broadcast message with the result of this operation.
Are there concurrency problem and I have to use : synchronized method and atomic variable or it doesn't matter ?
I reflect on this problem, because there isn't atomic variable for float primitive , and because I'm scared to block Ui thread with the wrong code..
EDIT: Other question
Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double).
from : https://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html
Then the int,double primitive type are atomic right ? The problem is on long and double.
For example : class test {
int c=0;
long p=0;
new Thread1( new Runnable(){
public void run(){
a=a+c;
p=p+c;
}
}
).start();
new Thread2( new Runnable(){
public void run(){
c=function();
p=functionx();
}
}
).start();
....
}
do I have to use volatile int for the visibility problems between threads or it doensn't matter ? and for the long variable should I use the atomicLong because the operations on the long primitive aren't atomic ?
p.s : I don't really understand the variable visibility problem between threads
AtomicReference
class that can atomically get/set pretty much any type of object. – MH.