synchronized

Is entering synchronized block atomic?

只谈情不闲聊 提交于 2019-12-21 17:38:09
问题 Do you know if there is guaranteed that synchronized block in java is atomic? Imagine following case Thread_1,2: synchronized(object){object.modify();} (object is shared variable.) imagine thread_M will change reference to object like synchronized(object){object = new Object()} now imagine threads 1 and 2 are competing over getting the lock on object Is it possible that following would happened: 1. Thread1: read old object 2. ThreadM: modify object reference & release old object lock 3.

Necessity of synchronized on local variable

纵然是瞬间 提交于 2019-12-21 10:29:10
问题 In the JSON-java library (org.json.JSONArray) I have found this code snippet with a synchronized block around a method-local variable public String toString(int indentFactor) throws JSONException { StringWriter sw = new StringWriter(); synchronized (sw.getBuffer()) { return this.write(sw, indentFactor, 0).toString(); } } I do not understand the necessity for the synchronization here, as the StringWriter is only local to the given method (and, why the synchronization is on the Buffer). Is the

Synchronized and local copies of variables

天涯浪子 提交于 2019-12-21 07:15:10
问题 I'm looking at some legacy code which has the following idiom: Map<String, Boolean> myMap = someGlobalInstance.getMap(); synchronized (myMap) { item = myMap.get(myKey); } The warning I get from Intelli-J's code inspections is: Synchronization on local variable 'myMap' Is this the appropriate synchronization and why? Map<String, Boolean> myMap = someGlobalInstance.getMap(); synchronized (someGlobalInstance.getMap()) { item = myMap.get(myKey); } 回答1: The reason this is flagged as a problem is

Synchronized and local copies of variables

£可爱£侵袭症+ 提交于 2019-12-21 07:15:03
问题 I'm looking at some legacy code which has the following idiom: Map<String, Boolean> myMap = someGlobalInstance.getMap(); synchronized (myMap) { item = myMap.get(myKey); } The warning I get from Intelli-J's code inspections is: Synchronization on local variable 'myMap' Is this the appropriate synchronization and why? Map<String, Boolean> myMap = someGlobalInstance.getMap(); synchronized (someGlobalInstance.getMap()) { item = myMap.get(myKey); } 回答1: The reason this is flagged as a problem is

并发和多线程

旧街凉风 提交于 2019-12-21 07:08:59
并发(concurrency):某段时间内,多个任务被CPU交替处理。 并行(parallelism):CPU同时处理多个任务。 并发打破和程序的封闭性,具有以下挑战: 1、并发程序之间有相互制约(对资源的争抢和彼此的依赖)的关系。 2、并发线程执行过程不连贯、断断续续,需要保存和切换现场。 3、设计合理的并发数,充分并合理利用CPU的执行能力才能真正发挥并发的优势。 线程数量并不是越多越好,要和相应的CPU资源匹配,合适的线程数才能充分利用计算资源。 线程的五个状态:NEW新建状态、RUNNABLE就绪状态、RUNNING运行状态、BLOCKED阻塞状态、DEAD终止状态。 线程被创建且未启动时的状态为NEW;线程调用start()方法之后进入就绪状态为RUNNABLE;线程被分配CPU时间片,开始执行run()方法,线程进入执行状态RUNNING;线程由于某些原因会进入阻塞状态BLOCKED;线程执行完毕,退出run()方法或异常退出即进入终止态DEAD。 有三类原因会让RUNNING中的线程变成BLOCKED:等待阻塞(调用wait方法)、主动阻塞(通过sleep、join主动让出CPU执行权)、同步阻塞(需要等待其他线程释放锁,I/O阻塞、同步块阻塞、锁阻塞) 线程从BLOCKED状态通过“苏醒(sleep时间到了)”、“被唤醒(notify)”、“获得锁

Thread.sleep() with synchronization in java

三世轮回 提交于 2019-12-21 05:51:39
问题 when Thread.sleep(10000) is invoked current Thread will go to sleeping state. If Thread.sleep(10000) is invoked in synchronization method whether other thread can execute in that period? 回答1: If you do Thread.sleep(10000) within a synchronized method or block you do not release the lock. Hence if other Threads are waiting on that lock they won't be able to execute. If you want to wait for a specified amount of time for a condition to happen and release the object lock you need to use Object

Thread.sleep() with synchronization in java

↘锁芯ラ 提交于 2019-12-21 05:51:06
问题 when Thread.sleep(10000) is invoked current Thread will go to sleeping state. If Thread.sleep(10000) is invoked in synchronization method whether other thread can execute in that period? 回答1: If you do Thread.sleep(10000) within a synchronized method or block you do not release the lock. Hence if other Threads are waiting on that lock they won't be able to execute. If you want to wait for a specified amount of time for a condition to happen and release the object lock you need to use Object

Should I synchronize listener notifications, or not?

戏子无情 提交于 2019-12-21 05:41:15
问题 I am always very hesitant to bring my locks in the open, to make them public. I always try to keep the locks restricted to my implementation. Not doing that, is a recipe for deadlocks, I believe. I have the following class: class SomeClass { protected ArrayList<Listener> mListeners = new ArrayList<Listener>(); protected void addListener(Listener listener) { synchronized (mListeners) { mListeners.add(listener); } } protected void removeListener(Listener listener) { synchronized (mListeners) {

单例模式有几种写法?

六眼飞鱼酱① 提交于 2019-12-21 05:06:12
单例模式有几种写法? 前言 纠结单例模式有几种写法有用吗?有点用,面试中经常选择其中一种或几种写法作为话头,考查设计模式和coding style的同时,还很容易扩展到其他问题。 这里讲解几种笔者常用的写法,但切忌生搬硬套,去记“茴香豆的写法”。编程最大的乐趣在于“know everything, control everything”。 JDK版本:oracle java 1.8 大体可分为4类,下面分别介绍他们的基本形式、变种及特点。 饱汉模式 饱汉是变种最多的单例模式。 基础的饱汉 饱汉,即已经吃饱,不着急再吃,饿的时候再吃。所以他就先不初始化单例,等第一次使用的时候再初始化,即“懒加载”。 // 饱汉 // UnThreadSafe public class Singleton1 { private static Singleton1 singleton = null; private Singleton1() { } public static Singleton1 getInstance() { if (singleton == null) { singleton = new Singleton1(); } return singleton; } } 饱汉模式的核心就是懒加载。好处是更启动速度快、节省资源,一直到实例被第一次访问,才需要初始化单例;小坏处是写起来麻烦

pthread_mutex_t VS @synchronized block?

眉间皱痕 提交于 2019-12-21 04:55:28
问题 static pthread_mutex_t gLock; //global pthread_mutex_init(&gLock,NULL); //in init pthread_mutex_lock(&gLock); for(int i=0;i<[message count];i++) CFSetAddValue(mSet, [message objectAtIndex:i]); pthread_mutex_unlock(&gLock); My cocoa application is going in not responding mode with pthread_mutex_t. @synchronized(mSet) { for(int i=0;i<[message count];i++) CFSetAddValue(mSet, [message objectAtIndex:i]); } My application is working fine with synchronized block. Why? 回答1: You're comparing a global