volatile

Java多线程面试题

醉酒当歌 提交于 2019-12-05 02:22:31
   1、启动一个线程是调用run()方法还是start()方法?      启动一个线程是调用start()方法,是线程所代表的虚拟处理机处于可运行状态,这意味着它可以由JVM调度并执行,这并不意味着线程就会立即执行   2、请说出同步线程及线程调度相关的方法?     wait():是一个线程等待(阻塞bolcked)状态,并且释放所持有的对象的锁     sleep():是一个正在运行状态的线程处于睡眠状态,是一个静态方法,调用此方法要处理InterruptedException异常;     notify():唤醒一个处于等待状态的线程,当然在调用此方法的时候,并不能确切的唤醒某一等待状态的线程,而是由JVM确定唤醒哪个线程,而且与优先级无关。     notifyAll():唤醒所有处于等待状态的线程,该方法并不是将对象的锁给所有线程,而是让它们竞争,只有获得了锁的线程才能进入就绪状态。     注意:Java5通过Lock接口提供了 显式的锁机制 ,Lock接口中定义了加锁(lock()方法)和解锁(unlock()方法),增强了多线程编程的灵活性及对线程的协调。???    3、线程和进程的区别?     进程:具有一定独立功能的程序关于某个数据集合上的一次运行活动,是操作系统进行资源分配和调度的一个独立单位。     线程:是进程的一个实体

Making variables captured by a closure volatile

扶醉桌前 提交于 2019-12-05 01:26:46
How do variables captured by a closure interact with different threads? In the following example code I would want to declare totalEvents as volatile, but C# does not allow this. (Yes I know this is bad code, it's just an example) private void WaitFor10Events() { volatile int totalEvents = 0; // error CS0106: _someEventGenerator.SomeEvent += (s, e) => totalEvents++; while(totalEvents < 10) Thread.Sleep(100); } EDIT : People seem to be missing the point of my question a bit. I know I can't use volatile on local vars. I also know that the example code code is bad and could be implemented in

Preferring synchronized to volatile

ぃ、小莉子 提交于 2019-12-05 00:52:17
I've read this answer in the end of which the following's written: Anything that you can with volatile can be done with synchronized, but not vice versa. It's not clear. JLS 8.3.1.4 defines volatile fields as follows: A field may be declared volatile, in which case the Java Memory Model ensures that all threads see a consistent value for the variable (§17.4). So, the volatile fields are about memory visibility. Also, as far as I got from the answer I cited, reading and writing to volatile fields are synched. Synchronization, in turn guarantees that the only one thread has access to a synched

Java面试参考指南

谁说胖子不能爱 提交于 2019-12-05 00:15:26
Java面向对象相关概念 Java是一种基于面向对象概念的编程语言,使用高度抽象化来解决现实世界的问题。 面向对象的方法将现实世界中的对象进行概念化,以便于在应用之间进行重用。例如:椅子、风扇、狗和电脑等。 Java里的类(Class)是一个蓝图、模板,或者称之为原型,它定义了同一类事物的相同属性和行为。实例(Instance)是某个类的一个具体实现,同一个类所有的实例拥有相同的属性。举例来说,你可以定义一个类叫做“房子(House)”,这个类拥有一个属性叫做“房间数(number of room)”,这样你就可以创建一个“房间数”为2的“房子”实例,你还可以创建一个“房间数”为3的“房子”实例,等等等等。 优点: 面向对象软件开发的若干优点在于: 模块化,维护成本低; 更好的代码重用,具备继承性,开发更为敏捷; 更好的代码可靠性和灵活性; 对现实世界进行建模,易于理解; 对象水平的抽象; 从一个开发阶段向另一个开发阶段过渡更为简便。 面向对象软件系统(OOPS)的四大主要特征为: 封装(Encapsulation) 继承(Inheritance) 多态(Polymorphism) 抽象(Abstraction) 封装(Encapsulation) 封装机制在对象之间提供了一种隐藏域可见性的协议。Java中使用可见性修饰符private将方法和变量限制在类内部

Java中的volatile关键字-转载

吃可爱长大的小学妹 提交于 2019-12-05 00:14:12
关于volatile, 我们知道,在Java中设置变量值的操作,除了long和double类型的变量外都是原子操作,也就是说,对于变量值的简单读写操作没有必要进行同步。这在JVM 1.2之前,Java的内存模型实现总是从主存读取变量,是不需要进行特别的注意的。而随着JVM的成熟和优化,现在在多线程环境下volatile关键字的使用变得非常重要。在当前的Java内存模型下,线程可以把变量保存在本地内存(比如机器的寄存器)中,而不是直接在主存中进行读写。这就可能造成一个线程在主存中修改了一个变量的值,而另外一个线程还继续使用它在寄存器中的变量值的拷贝,造成数据的不一致。要解决这个问题,只需要像在本程序中的这样,把该变量声明为volatile(不稳定的)即可 , 这就指示JVM,这个变量是不稳定的,每次使用它都到主存中进行读取。一般说来,多任务环境下各任务间共享的标志都应该加volatile修饰 。 Volatile修饰的成员变量在每次被线程访问时,都强迫从共享内存中重读该成员变量的值。而且,当成员变量发生变化时,强迫线程将变化值回写到共享内存。这样在任何时刻,两个不同的线程总是看到某个成员变量的同一个值。 Java语言规范中指出:为了获得最佳速度,允许线程保存共享成员变量的私有拷贝,而且只当线程进入或者离开同步代码块时才与共享成员变量的原始值对比。 这样当多个线程同时与某个对象交互时

为什么不应该使用“volatile”类型

百般思念 提交于 2019-12-05 00:14:02
以下文档来自于内核linux-2.6.32.6\Documentation\zh_CN\volatile-considered-harmful.txt C程序员通常认为volatile表示某个变量可以在当前执行的线程之外被改变;因此,在内核 中用到共享数据结构时,常常会有C程序员喜欢使用volatile这类变量。换句话说,他们经 常会把volatile类型看成某种简易的原子变量,当然它们不是。在内核中使用volatile几 乎总是错误的;本文档将解释为什么这样。 理解volatile的关键是知道它的目的是用来消除优化,实际上很少有人真正需要这样的应 用。在内核中,程序员必须防止意外的并发访问破坏共享的数据结构,这其实是一个完全 不同的任务。用来防止意外并发访问的保护措施,可以更加高效的避免大多数优化相关的 问题。 像volatile一样,内核提供了很多原语来保证并发访问时的数据安全(自旋锁, 互斥量,内 存屏障等等),同样可以防止意外的优化。如果可以正确使用这些内核原语,那么就没有 必要再使用volatile。如果仍然必须使用volatile,那么几乎可以肯定在代码的某处有一 个bug。在正确设计的内核代码中,volatile能带来的仅仅是使事情变慢。 思考一下这段典型的内核代码: spin_lock(&the_lock); do_something_on(&shared_data

understanding of Volatile.Read/Write

喜你入骨 提交于 2019-12-05 00:11:56
I'm trying to understand the C# Volatile class. As i read: The Volatile.Write method forces the value in location to be written to at the point of the call. In addition, any earlier program-order loads and stores must occur before the call to Volatile.Write. The Volatile.Read method forces the value in location to be read from at the point of the call. In addition, any later program-order loads and stores must occur after the call to Volatile.Read. Is that means the in the case of: internal sealed class ThreadsSharingData { private Int32 m_flag = 0; private Int32 m_value = 0; // This method is

difference between Memory Barriers and lock prefixed instruction

别等时光非礼了梦想. 提交于 2019-12-04 23:32:22
In this article Memory Barriers and JVM Concurrency !, i was told volatile is implemented by different memory barriers instructions,while synchronized and atomic are implemented by lock prefixed instruction. But i get bellow code in some other article: java code: volatile Singleton instance = new Singleton(); assembly instruction(x86): 0x01a3de1d: movb $0x0,0x1104800(%esi); 0x01a3de24: lock addl $0x0,(%esp); So which one is right?And what is the difference between Memory Barriers and lock prefixed instruction without considering my poor english? Short answer Lock instructions are used to

Volatile fields: How can I actually get the latest written value to a field?

帅比萌擦擦* 提交于 2019-12-04 22:59:32
问题 Considering the following example: private int sharedState = 0; private void FirstThread() { Volatile.Write(ref sharedState, 1); } private void SecondThread() { int sharedStateSnapshot = Volatile.Read(ref sharedState); Console.WriteLine(sharedStateSnapshot); } Until recently, I was under the impression that, as long as FirstThread() really did execute before SecondThread() , this program could not output anything but 1 . However, my understanding now is that: Volatile.Write() emits a release

In C, how do you declare the members of a structure as volatile?

大憨熊 提交于 2019-12-04 22:29:06
How do you declare a particular member of a struct as volatile? Exactly the same as non- struct fields: #include <stdio.h> int main (int c, char *v[]) { struct _a { int a1; volatile int a2; int a3; } a; a.a1 = 1; a.a2 = 2; a.a3 = 3; return 0; } You can mark the entire struct as volatile by using "volatile struct _a {...}" but the method above is for individual fields. Should be pretty straight forward according to this article: Finally, if you apply volatile to a struct or union, the entire contents of the struct/union are volatile. If you don't want this behavior, you can apply the volatile