volatile

Control of running Thread using multiple threading concept of java

守給你的承諾、 提交于 2019-12-06 06:34:43
I just want to start and stop the thread when return key is pressed. Here thread is stopped fine but i cant Start that thread again please help. Also explain me the use of volatile keyword .Is it helpful for me to over come this problem. public class Sync extends Thread{ public boolean meth= true; public void run(){ while(meth){ System.out.println("hello"); try { Thread.sleep(1000); } catch (InterruptedException ex) { } } } public void shutdown(){ meth=false; } public void startup(){ meth=true; } } MAIN CLASS`` package com.Sync; import java.util.Scanner; public class SyncMain { public static

Workings of AtomicReferenceArray

主宰稳场 提交于 2019-12-06 05:11:10
I am wondering if AtomicReferenceArray can be used as a replacement for ConcurrentLinkedQueue (if one could live with a bounded structure). I currently have something like: ConcurrentLinkedQueue<Object[]> queue = new ConcurrentLinkedQueue<Object[]>(); public void store(Price price, Instrument instrument, Object[] formats){ Object[] elements = {price, instrument, formats}; queue.offer( elements); } The store(..) is called by multiple threads. I also have a consumer thread, which periodically wakes up and consumes the elements. private class Consumer implements Runnable{ @Override public void

volatile 关键字

≡放荡痞女 提交于 2019-12-06 04:54:53
一、cpu cache 模型 cpu 与 主存的速度差异因此产生了缓存。现在缓存的数量增加到3 级,最靠近cpu 的 称为 L1,然后依次是 L2 ,L3 由于程序指令与数据的行为和热点分布差异, L1 Cache 分为 L1i 和 L1d 。 缓存提高了吞吐力,单引入了缓存不一致问题。 比如: i++ 读取主内存的 i 到 cache 对 i 进行 +1 将结果写回到 cpu cache 将数据写回到主存 在单线程是没有问题的,但是在多线程就会出现问题。如何解决缓存不一致问题? 01.通过总线加锁 02.通过缓存一致性协议    二、理解 Volatile 关键字 并发三个特性 原子性 有序性 可见性 原子性:操作是不可再分割的一个整体,原子操作+原子操作 == 非原子操作 可见性:当一个线程对共享变量修改,别的线程立马可以看到修改后的新值 有序性: 指代码执行过程的先后顺序,在多线程的情况下,如果不能有序,则会产生很大问题 Java 如何保障可见性? volatile 修饰,对于资源的操作会直接在主内存中进行 synchronized 保证可见性,同一时刻只能用一个线程可见 juc 中的 Lock ,与 synchronized 相同 Java 中如何保证有序性? volatile synchronzied Lock 使用 Volatile 的意义: 线程之间变量的可见性

Crashing threads with *(int*)NULL = 1; problematic?

Deadly 提交于 2019-12-06 03:53:42
问题 I found this in a multi-threaded c application. The authors commented that it's used to make a thread crash in a custom assert function. GCC is fine with it, but clang issues the following warning: note: consider using __builtin_trap() or qualifying pointer with 'volatile' and also issues one of those, for each usage of the assert function: warning: indirection of non-volatile null pointer will be deleted, not trap What is going on here? Is __builtin_trap specific to clang? Should I use it?

Sequential consistency volatile explanation

两盒软妹~` 提交于 2019-12-06 03:45:25
I am watching video from java jpoint conference. I have question about following slide from Alexey Shipilev report: Excuse me for non-english on slide. Actually author says that it is impossible that variable set will be r1 = 1 (Y) r2 = 0 (x) r3 = 1 (x) r4 = 0 (Y) According the video he implies that it is obviously. Can someone clarify why this value set impossible according JMM? P.S. If I understand Alexey notation correct it respects the following code: public class SequentialConsistency { static volatile int x; static volatile int y; public static void main(String[] args) { new Thread(new

并发编程—Volatile关键字

好久不见. 提交于 2019-12-06 03:42:34
锁提供了两种主要特性:互斥(mutual exclusion) 和可见性(visibility)。互斥即一次只允许一个线程持有某个特定的锁,因此可以保证一次就只有一个线程在访问共享数据。可见性要复杂一些,它必须确保释放锁之前对共享数据做出的更改对于随后获得该锁的另一个线程是可见的。 volatile 变量可以被看作是一种 “轻量级的 synchronized”,与 synchronized 块相比,volatile 变量所需的编码较少,并且运行时开销也较少,但是它所能实现的功能也仅是 synchronized 的一部分。 volatile变量 一个共享变量被volatile修饰之后,则具有了两层语义: 保证了该变量在多个线程的可见性(这是结果) 会通过Lock命令禁止特定类型的命令重排序(这是原因) volatile的实现原理 首先了解一下内存屏障(memory barriers)的概念,内存屏障是一组处理器命令,用于实现对内存操作的顺序限制。 如果代码中的共享变量被volatile修饰,在生成汇编代码时会在volatile修饰的共享变量进行写操作的时候会多出Lock前缀的命令。在多核处理器的情况下,这个Lock命令主要有3个功能: volatile的变量被修改后会立即写入到主存中 这个写回主存的操作会告知其它线程中该变量对应的缓存行失效,所以其它线程如果要操作这个变量

多线程学习笔记(十二)

放肆的年华 提交于 2019-12-06 03:31:03
volatile的作用是使变量在多个线程间可见 1.死循环 public class PrintInfo implements Runnable { private boolean statu = true; public boolean isStatu() { return statu; } public void setStatu(boolean statu) { this.statu = statu; } public void printInfo() { try { while(statu == true){ System.out.println("The Thread Name Is " + Thread.currentThread().getName()); Thread.sleep(1000); Thread.sleep(1000); } } catch (Exception e) { e.printStackTrace(); } } @Override public void run() { printInfo(); } } public class Run { public static void main(String[] args){ PrintInfo printInfo = new PrintInfo(); printInfo.printInfo();

When can a volatile variable be optimized away completely?

谁说胖子不能爱 提交于 2019-12-06 02:19:18
问题 Consider this code example: int main(void) { volatile int a; static volatile int b; volatile int c; c = 20; static volatile int d; d = 30; volatile int e = 40; static volatile int f = 50; return 0; } Without volatile a compiler could optimize away all the variables since they are never read from. I think a and b can be optimized away since they are completely unused, see unused volatile variable. I think c and d can not be removed since they are written to, and writes to volatile variables

volatile关键字的作用是什么?

▼魔方 西西 提交于 2019-12-06 01:56:13
Java 中 volatile 关键字是一个类型修饰符。JDK 1.5 之后,对其语义进行了增强。 保证了不同线程对共享变量进行操作时的可见性,即一个线程修改了共享变量的值,共享变量修改后的值对其他线程立即可见 通过禁止编译器、CPU 指令重排序和部分 happens-before 规则,解决有序性问题 volatile 可见性的实现 在生成汇编代码指令时会在 volatile 修饰的共享变量进行写操作的时候会多出 Lock 前缀的指令 Lock 前缀的指令会引起 CPU 缓存写回内存 一个 CPU 的缓存回写到内存会导致其他 CPU 缓存了该内存地址的数据无效 volatile 变量通过缓存一致性协议保证每个线程获得最新值 缓存一致性协议保证每个 CPU 通过嗅探在总线上传播的数据来检查自己缓存的值是不是修改 当 CPU 发现自己缓存行对应的内存地址被修改,会将当前 CPU 的缓存行设置成无效状态,重新从内存中把数据读到 CPU 缓存 看一下我们之前的一个可见性问题的测试例子 package constxiong.concurrency.a014; /** * 测试可见性问题 * @author ConstXiong */ public class TestVisibility { //是否停止 变量 private static boolean stop = false;

java Volatile/synchronization on arraylist

我的未来我决定 提交于 2019-12-06 01:38:28
My program looks like this: public class Main { private static ArrayList<T> list; public static void main(String[] args) { new DataListener().start(); new DataUpdater().start(); } static class DataListener extends Thread { @Override public void run() { while(true){ //Reading the ArrayList and displaying the updated data Thread.sleep(5000); } } } static class DataUpdater extends Thread{ @Override public void run() { //Continuously receive data and update ArrayList; } } } In order to use this ArrayList in both threads, I know two options: To make the ArrayList volatile. However I read in this