synchronized

Clarification on the meaning of the parameter block synchronization

血红的双手。 提交于 2019-12-10 12:17:20
问题 i would like to know if this expression it is correct and if it means this: i put a write lock over the field status, and than i change it. If not, i would like to know what is the meaning of the paramenter, because i see always this . public class Example { private int status; public Example(int status){ this.status = status; } public void setStatus(int newStatus){ synchronized(this.status){ this.status = newStatus; } } } 回答1: There are several things wrong with this code: You cannot

Garbage collection and synchronized visibility

[亡魂溺海] 提交于 2019-12-10 11:46:43
问题 I have read about marking an object as volatile doesn't guarantee visibility of it's members ( I'm not saying about thread safety just memory visibility , quoting : only the object reference will be considered to be volatile by the JVM and not the object data itself which will reside on the heap my questions : The synchronize will ensure the visibility of the members (on the same lock object) in case they have been edited. Is that because the happens-before at the end (release) of the lock

synchronized not synchronizing

六眼飞鱼酱① 提交于 2019-12-10 11:28:18
问题 synchronized does not appear to work as I had expected. Shouldn't it make the code within the brackets atomic with respect to a separate block of code that is synchronized by the same object? I'm finding that it's not synchronizing at all within my code. private Object movementMutex_ = new Object(); // Thread public void run() { while (run_) { synchronized(movementMutex_) { if (timeToMove_) { Log.v("meh", "timeToMove_ was true, moving"); makeMove(); Log.v("meh", "Move Complete. Setting

Why can't I directly access (and lock) the implicit lock that Objects use for synchronized block

懵懂的女人 提交于 2019-12-10 10:34:09
问题 rephrased for clarity I would like to be able to mix the use of the synchronized block with more explicit locking via calling lock and release methods directly when appropriate. Thus allowing me the syntaxtical sugar of using sychtronized(myObject) when I can get away with it, but also being able to call myObject.lock & myObject.unlock dierctly for those times when a synchronized block is not flexible enough to do what I need done. I know that every single Object has, implicitly, a rentrant

Multiple object locks in Java?

偶尔善良 提交于 2019-12-10 10:29:41
问题 Is it safe/acceptable practice to lock on a private field variable (instead of using a lock object)? This way, I could have different locks for different purposes. Example below: class Test { private Integer x = 0; private Integer y = 0; public void incrementX() { synchronized(x) { x++; } } public void decrementX() { synchronized(x) { x++; } } public void incrementY() { synchronized(y) { y++; } } public void decrementY() { synchronized(y) { y++; } } Or should I have a lock object for each

synchronized和Lock有什么区别?

北城余情 提交于 2019-12-10 10:06:04
实现层面不一样。synchronized 是 Java 关键字,JVM层面 实现加锁和释放锁;Lock 是一个接口,在代码层面实现加锁和释放锁 是否自动释放锁。synchronized 在线程代码执行完或出现异常时自动释放锁;Lock 不会自动释放锁,需要再 finally {} 代码块显式地中释放锁 是否一直等待。synchronized 会导致线程拿不到锁一直等待;Lock 可以设置尝试获取锁或者获取锁失败一定时间超时 获取锁成功是否可知。synchronized 无法得知是否获取锁成功;Lock 可以通过 tryLock 获得加锁是否成功 功能复杂性。synchronized 加锁可重入、不可中断、非公平;Lock 可重入、可判断、可公平和不公平、细分读写锁提高效率 原文链接 Java 自学指南 Java 面试题汇总PC端浏览【点这里】 Java知识图谱 Java 面试题汇总小程序浏览,扫二维码 所有资源 资源汇总于公众号 来源: https://www.cnblogs.com/ConstXiong/p/12014894.html

多线程学习(5)volatile 和 synchronized 的区别

自闭症网瘾萝莉.ら 提交于 2019-12-10 09:59:51
满足同步三个基本条件: 1.原子性 原子性是指操作是不可分的。其表现在于对于共享变量的某些操作,应该是不可分的,必须连续完成。例如a++,对于共享变量a的操作,实际上会执行三个步骤,1.读取变量a的值 2.a的值+1 3.将值赋予变量a 。 这三个操作中任何一个操作过程中,a的值被人篡改,那么都会出现我们不希望出现的结果。所以我们必须保证这是原子性的。 2.可见性:一个线程对共享变量值得修改,能够及时的被其他线程看到 多线程高并发中,线程之间的通信是靠内存共享,即一个对象或变量,它在堆中的主内存中,每一个请求可以看做一个线程,每一个线程,都想操作这个主内存的对象或变量,线程会复制一份主内存中的对象或变量,到自己的线程本地栈内存中,直到操作完毕,才会将本地栈内存的对象或变量,赋值给主内存。如果不加同步锁,主内存中的对象或变量,值就容易被最后提交操作的线程覆盖,发生错误。 3.有序性: 指令重排序:代码书写的顺序与实际执行的顺序不同,指令重排序是编译器或处理器为了提高程序性能而做的优化。 1.编译器优化的重排序(编译器优化) 2.指令级并行重排序(处理器优化) 3.内存系统的重排序(处理器优化) 是不是所有的语句的执行顺序都可以重排呢? 答案是否定的。为了讲清楚这个问题,先讲解另一个概念:数据依赖性 什么是数据依赖性? 如果两个操作访问同一个变量,且这两个操作中有一个为写操作

synchronized和ReentrantLock区别

*爱你&永不变心* 提交于 2019-12-10 03:10:05
一.什么是 sychronized sychronized是java中最基本同步互斥的手段,可以修饰代码块,方法,类. 在修饰代码块的时候需要一个reference对象作为锁的对象. 在修饰方法的时候默认是当前对象作为锁的对象. 在修饰类时候默认是当前类的Class对象作为锁的对象. synchronized会在进入同步块的前后分别形成monitorenter和monitorexit字节码指令.在执行monitorenter指令时会尝试获取对象的锁,如果此没对象没有被锁,或者此对象已经被当前线程锁住,那么锁的计数器加一,每当monitorexit被锁的对象的计数器减一.直到为0就释放该对象的锁.由此synchronized是可重入的,不会出现自己把自己锁死. 二.什么ReentrantLock 以对象的方式来操作对象锁.相对于sychronized需要在finally中去释放锁 三.synchronized和ReentrantLock的区别 除了synchronized的功能,多了三个高级功能. 等待可中断,公平锁,绑定多个Condition. 1.等待可中断 在持有锁的线程长时间不释放锁的时候,等待的线程可以选择放弃等待. tryLock(long timeout, TimeUnit unit) 2.公平锁 按照申请锁的顺序来一次获得锁称为公平锁

全面解析Synchronized

橙三吉。 提交于 2019-12-10 03:09:51
在多线程的环境下,经常存在线程安全问题,这种问题产生的原因在于:该是原子操作的代码段被其他的线程切割,从而引起的数据混乱问题。在本篇博客中将讲述如何使用synchronized关键字保证代码段的原子操作。 synchronized关键字 不管synchronized以何种方式使用,都会对一个对象加锁,这个对象也就是所谓的监视器 synchronized关键字具有一下特征: (1)如果对持有相同锁的synchronized方法或者代码块,同步执行(即排队,执行完一个,另一个才能执行) (2)如果对持有不同锁的synchronized方法 或者代码块 ,异步执行 (3)synchronized与非synchronized方法 或者代码块 ,异步执行 synchronized方法 使用synchronized来修饰方法(非static方法),其实就是this对象加锁 下面通过synchronized方法来看一下上述的三个特征 首先定义一个操作类MyObject,在类中有三个方法init,alter,print,其中init,alter方法使用synchronized修饰,代码如下: package com.feng.example; public class MyObject { synchronized public void init() { try { System.out

synchronized 之 对象锁 和 类锁

本小妞迷上赌 提交于 2019-12-10 02:50:27
一、synchronized(object) 如果object没有被加锁,则获取object的锁;如果object已经被加锁则等待object的锁被释放。 二、需要加锁的情景 多线程共享同一资源会引起线程安全的情况下,才需要加同步锁。不同的对象在不同的线程中时(如:A类的A1对象,和A类的A2对象,分别在线程a1和a2中),是不存在线程安全问题的。 三、synchronized的几种用法或者是形式 大致可以分为两种情况 synchronized修饰非静态方法、同步代码块的synchronized (this)用法和synchronized (非this对象) synchronized修饰静态方法以及同步代码块的synchronized (类.class) 四、理解 synchronized修饰非静态方法 和 synchronized(this) 用synchronized修饰方法和用synchronized(this)获取的是该对象的锁 synchronized同步方法 对其它的synchronized同步方法或synchronized(this)同步代码块调用是堵塞状态; 同一时间只有一个线程执行synchronized同步方法中的代码。 synchronized(this)同步代码块 对其它的synchronized同步方法或synchronized(this