volatile

java个人收集面试题

喜夏-厌秋 提交于 2019-11-30 08:42:01
java基础 private修饰的方法可以通过反射访问,那么private的意义是什么? (1).Java的private修饰符不是为了绝对安全设计的,而是对用户常规使用Java的 一种约束。就好比饭店厨房门口挂着“闲人免进”的牌子,但是你还是能够通过其他方法进去。 (2)、从外部对对象进行常规调用时,能够看到清晰的类结构 java类的初始化顺序? (1)不考虑继承的情况下 写一个测试类验证: public class LoadOrderHelp { static { System . out . println ( "1:静态块" ) ; b = 1 ; //这一句报错(Illegal forward reference ),即不能在b没有声明初始化前输出,到可以赋值。咦! //System.out.println(b); } public static int b = 10 ; //静态字段初始化 static { System . out . println ( "2: 静态块 static b = " + b ) ; b = 5 ; } private int a = 1 ; //非静态字段初始化 { System . out . println ( "3:非静态块 a = " + a + " b = " + b ) ; } { System . out . println

java.util.concurrent.atomic随笔及volatile语义

≡放荡痞女 提交于 2019-11-30 08:20:40
一个原子操作( atomic operation )是个不能分割的整体,没有其它线程( thread )能够中断或检查正在原子操作中的变量。一个原子( atomic )类型就是一个原子操作可用的类型,它可以在基本上没有锁( lock )的情况下做到线程安全( thread-safe )。 java .util.concurrent.atomic 包就是提供原子操作的类的小工具包,支持在单个变量上解除锁定的线程安全编程。包中的类将 volatile 值、字段和数组元素的概念扩展到那写也支持原子条件更新操作的类。如 AtomicReference ,一个“可以用原子方式更新的对象引用”。 JDK 的文档中说:“ 设计原子类主要用作各种块,用于实现非阻塞数据结构和相关基础结构类 。 compareAndSet() 方法不是锁定的常规替换方法。仅当对象的重要更新限于单个变量时才应用它” 类图解: ?从类图中可以清晰的得出:这些基本都是实现 java 常用的基本类型及他们的数组的原子实现。 volatile语义 volatile相当于synchronized的弱实现,也就是说volatile实现了类似synchronized的语义,却又没有锁机制。它确保对volatile字段的更新以可预见的方式告知其他的线程。 volatile包含以下语义: (1) Java

Volatile for reference type - Does it always avoid publication of references issues due to JMM?

我只是一个虾纸丫 提交于 2019-11-30 07:24:40
Assuming this class: public class AmIThreadSafe { private int a; private int b; AmIThreadSafe(int a, int b) { this.a = a; this.b = b; } } Assuming that instance's reference to this class (declared as volatile ) is accessible by some threads (leading to race condition) as soon as the this (reference) escapes: volatile AmIThreadSafe instance = new AmIThreadSafe(1,2); Here, I'm sure that the fact of assigning instance reference happens-before reading by threads. But what about the AmIThreadSafe's fields? Does the external volatile keyword also imply an happens-before relation concerning a and b

In Java, is it safe to change a reference to a HashMap read concurrently

五迷三道 提交于 2019-11-30 07:04:26
I hope this isn't too silly a question... I have code similar to the following in my project: public class ConfigStore { public static class Config { public final String setting1; public final String setting2; public final String setting3; public Config(String setting1, String setting2, String setting3) { this.setting1 = setting1; this.setting2 = setting2; this.setting3 = setting3; } } private volatile HashMap<String, Config> store = new HashMap<String, Config>(); public void swapConfigs(HashMap<String, Config> newConfigs) { this.store = newConfigs; } public Config getConfig(String name) {

C# volatile double

帅比萌擦擦* 提交于 2019-11-30 06:58:32
问题 As only reference types and a few primitives (including float, but not double, I'm not sure the reason why, I'm happy to hear why) can be declared as volatile, if I wrap a double in a class then declare it as volatile (as below), will the double property be 'read write' thread safe as any other volatile, or should I still be looking at locking? public class MyThreadedClass { volatile VolatileDouble voldub; } public class VolatileDouble { public double Double { get; set; } } 回答1: Reason why

Why aren't variables in Java volatile by default?

时光毁灭记忆、已成空白 提交于 2019-11-30 06:53:45
Possibly similar question: Do you ever use the volatile keyword in Java? Today I was debugging my game; It had a very difficult threading problem that would show up every few minutes, but was difficult to reproduce. So first I added the synchronized keyword to each of my methods. That didn't work. Then I added the volatile keyword to every field. The problem seemed to just fix itself. After some experimentation I found that the field responsible was a GameState object which kept track of my game's current state, which can be either playing or busy. When busy, the game ignores user input. What

I/O模型之阻塞(在中断基础上------jz2440)

江枫思渺然 提交于 2019-11-30 04:25:20
1 阻塞: 在应用层调用read函数的时候,如果硬件中的数据没有准备好,此时进程会进入休眠状态,当硬件的数据准备好的时候会给驱动发送中断。驱动收到中断之后,唤醒休眠的进程。这个被唤醒的进程在driver_read读取硬件的数据,并把数据 返回到用户空间。(模型中断) 可以使用队列,把整个进程进入到队列中,使进程进入阻塞的状态,当有数据产生的时候,产生中断,唤醒在队列中的进程,从而读取数据. 队列: wait_queue_head_t wq //定义等待队列头 init_waitqueue_head(&wq) //初始化等待队列头 wait_event(wq, condition) //不可中断的等待态 wait_event_interruptible (wq, condition)//可中断的等待态(当产生中断信号就可以唤醒) 参数: @wq :等待对列头 @condition :如果条件为假表示可休眠,如果为真表示不休眠 返回值:成功返回0,失败返回错误码 wake_up(&wq) wake_up_interruptible(&wq) 唤醒休眠 condition = 1; 分析:wake_up_interruptible()唤醒后,wait_event_interruptible(wq, condition)宏,自身再检查“condition”这个条件以决定是返回还是继续休眠

Is it legal to optimize away stores/construction of volatile stack variables?

旧街凉风 提交于 2019-11-30 04:22:26
问题 I noticed that clang and gcc optimize away the construction of or assignment to a volatile struct declared on the stack, in some scenarios. For example, the following code: struct nonvol2 { uint32_t a, b; }; void volatile_struct2() { volatile nonvol2 temp = {1, 2}; } Compiles on clang to: volatile_struct2(): # @volatile_struct2() ret On the other hand, gcc does not remove the stores, although it does optimize the two implied stores into a single one: volatile_struct2(): movabs rax, 8589934593

Synchronize write access to Volatile field (Cheap read-write block)

☆樱花仙子☆ 提交于 2019-11-30 04:13:44
问题 Let's say I have the following class that will be read heavily, but only written to occasionally. It will be used in a multi-threaded web app, so it needs to be thread safe: public class Foo { private volatile String foo; public String getFoo() { return foo; } public synchronized String setFoo(String in) { this.foo = in; } } Java Concurrency (http://www.ibm.com/developerworks/java/library/j-jtp06197/index.html) states that this is a fragile way to protect write access while improving read

java关键字volatile的作用

时间秒杀一切 提交于 2019-11-30 03:22:08
自从jdk1.5以后, volatile 可谓发生了翻天覆地的变化,从一个一直被吐槽的关键词,变成一个轻量级的线程通信代名词。 接下来我们将从以下几个方面来分析以下 volatile 。 重排序与 as if serial 的关系 volatile 的特点 volatile 的内存语义 volatile 的使用场景 重排序与as if serial的关系 重排序值得是编译器与处理器为了优化程序的性能,而对指令序列进行重新排序的。 但是并不是什么情况下都可以重排序的, 数据依赖 a = 1; // 1 b = 2; // 2 在这种情况,1、2不存在数据依赖,是可以重排序的。 a = 1; // 1 b = a; // 2 在这种情况,1、2存在数据依赖,是禁止重排序的。 as if serial 简单的理解就是。不管怎么重排序,在单线程情况下程序的执行结果是一致。 根据 as if serial 原则,它强调了单线程。那么多线程发生重排序又是怎么样的呢? 请看下面代码 public class VolatileExample1 { /** * 共享变量 name */ private static String name = "init"; /** * 共享变量 flag */ private static boolean flag = false; public static