AtomicBoolean做什么,一个volatile布尔无法实现?
我想知道在声明变量as volatile和始终访问synchronized(this)Java 中的块中的变量之间的区别?
根据这篇文章,http://www.javamex.com/tutorials/synchronization_volatile.shtml有很多要说的,但也有很多不同之处,但也有一些相似之处.
我对这条信息特别感兴趣:
...
- 对volatile变量的访问永远不会阻塞:我们只进行简单的读或写操作,因此与synchronized块不同,我们永远不会持有任何锁;
- 因为访问volatile变量永远不会持有锁,所以它不适合我们想要读取update-write作为原子操作的情况(除非我们准备"错过更新");
read-update-write是什么意思?写入也不是更新,还是仅仅意味着更新是依赖于读取的写入?
最重要的是,何时更适合声明变量volatile而不是通过synchronized块访问变量?使用volatile依赖于输入的变量是一个好主意吗?例如,有一个变量被称为render通过渲染循环读取并由按键事件设置?
我读到了下面的某个地方.
Java volatile关键字并不意味着原子,它常见的误解是,在声明volatile之后,
++操作将是原子的,要使操作原子化,你仍然需要确保使用synchronizedJava中的方法或块进行独占访问 .
那么如果两个线程同时攻击一个volatile原始变量会发生什么呢?
这是否意味着,凡发生在它的锁,将要设置其值.如果在此期间,一些其他的线程来和读取旧值,而第一个线程正在改变它的价值,那么没有新的线程将读取其旧的价值?
Atomic和volatile关键字有什么区别?
为什么i++Java中没有原子?
为了更深入地了解Java,我试图计算线程循环执行的频率.
所以我用了一个
private static int total = 0;
Run Code Online (Sandbox Code Playgroud)
在主要班级.
我有两个主题.
System.out.println("Hello from Thread 1!");System.out.println("Hello from Thread 2!");并且我计算由线程1和线程2打印的线.但是线程1的线+线程2的线与打印出的总线数不匹配.
这是我的代码:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Test {
private static int total = 0;
private static int countT1 = 0;
private static int countT2 = 0;
private boolean run = true;
public Test() {
ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
newCachedThreadPool.execute(t1);
newCachedThreadPool.execute(t2);
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); …Run Code Online (Sandbox Code Playgroud) private double value;
public synchronized void setValue(double value) {
this.value = value;
}
public double getValue() {
return this.value;
}
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,让getter同步有什么意义吗?
我知道volatile允许可见性,AtomicInteger允许原子性.所以,如果我使用volatile AtomicInteger,是否意味着我不必再使用任何同步机制?
例如.
class A {
private volatile AtomicInteger count;
void someMethod(){
// do something
if(count.get() < 10) {
count.incrementAndGet();
}
}
Run Code Online (Sandbox Code Playgroud)
这线程安全吗?
在下面的代码中,我有一个while(true)循环.考虑到try块中存在一些代码的情况,其中线程应该执行一些约需一分钟的任务,但是由于某些预期的问题,它正在运行.我们可以阻止那个线程吗?
public class thread1 implements Runnable {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
thread1 t1 = new thread1();
t1.run();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try{
Thread.sleep(10);
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
object Foo {
private var ctr = 0L
def bar = {
ctr = ctr + 1
// do something with ctr
}
}
Run Code Online (Sandbox Code Playgroud)
要求是ctr值只应使用一次.在我的情况下,相同的值ctr被重用.我的理论是,这是因为Foo.bar在不同的线程中同时调用(这是我可以得出的唯一结论).我有以下修改过的代码作为修复.
object Foo {
private var ctr = 0L
def getCtr = synchronized{
ctr = ctr + 1
ctr
}
def bar = {
val currCtr = getCtr
// do something with currCtr
}
}
Run Code Online (Sandbox Code Playgroud)
我找不到synchronized在Scala中使用该方法的好指南.任何人都可以告诉我,如果上述代码将解决我的问题.
编辑:基于以下评论,我认为AtomicLong对我来说是最好的解决方案:
import java.util.concurrent.atomic.AtomicLong
private val ctr = new AtomicLong …Run Code Online (Sandbox Code Playgroud) 我有两个主题:
主题:1
a = 1;
x = b;
Run Code Online (Sandbox Code Playgroud)
主题:2
b = 1
y = a
Run Code Online (Sandbox Code Playgroud)
这里a和b被声明为volatile.我不明白在a = 1之间如何创建"before-before"边缘; 和y = a; 在x = b之间; 和b = 1;
我知道通过使用volatile变量可以防止从线程缓存中读取过时值.但是,在订购之前,如何确保变量发生变化.
具体来说,我不明白这一点:
对每个后续读取相同字段之前发生对易失性字段的写入.
锄头有用吗?
以下是STM32微控制器上的数据类型:http : //www.keil.com/support/man/docs/armcc/armcc_chr1359125009502.htm。
这些微控制器使用32位ARM核心处理器。
哪些数据类型具有自动原子读取和原子写入访问权限?
我很确定所有32位数据类型都可以(因为处理器是32位),而所有64位数据类型都不能(因为要读或写一个64位字至少需要2个处理器操作),但是bool(1个字节)和uint16_t/ int16_t(2个字节)呢?
上下文:我正在STM32上的多个线程(在FreeRTOS中称为单核,但有多个线程或称为“任务”)之间共享变量,并且需要知道是否需要通过使用中断关闭中断来强制进行原子访问互斥锁等
更新:
参考此示例代码:
volatile bool shared_bool;
volatile uint8_t shared u8;
volatile uint16_t shared_u16;
volatile uint32_t shared_u32;
volatile uint64_t shared_u64;
volatile float shared_f; // 32-bits
volatile double shared_d; // 64-bits
// Task (thread) 1
while (1)
{
// Write to the values in this thread.
// What I write to each variable will vary. Since other threads
// are reading these values, I need to …Run Code Online (Sandbox Code Playgroud)