synchronized

Should I synchronize with self or with the method argument?

给你一囗甜甜゛ 提交于 2019-12-06 10:22:38
In a method like this, which do synchronize (i.e. self or thing )? - (BOOL)deleteThing:(MSThing *)thing error:(NSError **)error { @synchronized(self) { if (!thing) { return YES; } NSString *fileName = [[self thingDirectory] stringByAppendingPathComponent:thing.cacheInstanceName]; if (![[NSFileManager defaultManager] fileExistsAtPath:fileName]) { //... === OR === - (BOOL)deleteThing:(MSThing *)thing error:(NSError **)error { @synchronized(thing) { if (!thing) { return YES; } NSString *fileName = [[self thingDirectory] stringByAppendingPathComponent:thing.cacheInstanceName]; if (![[NSFileManager

java之线程

馋奶兔 提交于 2019-12-06 10:08:35
1、线程与进程    线程(thread):线程是运行的程序单元,依托于进程存在。一个进程可以包含多个线程,多线程可以共享一块内存空间和一组系统资源。   进程(processes):进程是程序的一次动态执行,通常每一个进程都拥有自己独立的内存空间和系统资源 2、线程的创建 继承Thread类,重写run方法 实现runnable接口,实现run方法 实现Callable接口,实现call方法 lambda表达式创建线程   1)、继承thread类,代码实现; class ThreadTest{ public static void main(String[] args) throws Exception{ MyThread thresd = new MyThread(); thread.start(); } } class MyThread extends Thread{ @Override public void run(){ System.out.println("123"): } }    2)、实现runnable接口,代码实现 class ThreadTest{ publiic static void main(String[] args){ MyRunnable runnable = new MyRunnable (); new Thread(runnable)

synchronized锁的升级原理是什么?

﹥>﹥吖頭↗ 提交于 2019-12-06 10:07:30
锁的级别从低到高: 无锁 -> 偏向锁 -> 轻量级锁 -> 重量级锁 锁分级别原因: 没有优化以前,sychronized是重量级锁(悲观锁),使用 wait 和 notify、notifyAll 来切换线程状态非常消耗系统资源;线程的挂起和唤醒间隔很短暂,这样很浪费资源,影响性能。所以 JVM 对 sychronized 关键字进行了优化,把锁分为 无锁、偏向锁、轻量级锁、重量级锁 状态。 无锁: 没有对资源进行锁定,所有的线程都能访问并修改同一个资源,但同时只有一个线程能修改成功,其他修改失败的线程会不断重试直到修改成功。 偏向锁: 对象的代码一直被同一线程执行,不存在多个线程竞争,该线程在后续的执行中自动获取锁,降低获取锁带来的性能开销。偏向锁,指的就是偏向第一个加锁线程,该线程是不会主动释放偏向锁的,只有当其他线程尝试竞争偏向锁才会被释放。 偏向锁的撤销,需要在某个时间点上没有字节码正在执行时,先暂停拥有偏向锁的线程,然后判断锁对象是否处于被锁定状态。如果线程不处于活动状态,则将对象头设置成无锁状态,并撤销偏向锁; 如果线程处于活动状态,升级为轻量级锁的状态。 轻量级锁: 轻量级锁是指当锁是偏向锁的时候,被第二个线程 B 所访问,此时偏向锁就会升级为轻量级锁,线程 B 会通过自旋的形式尝试获取锁,线程不会阻塞,从而提高性能。 当前只有一个等待线程

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

混江龙づ霸主 提交于 2019-12-06 08:00:27
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 lock built into it which is used by the synchronized block. Effectively the the lock mthod is caled on

Synchronized 原理

拈花ヽ惹草 提交于 2019-12-06 07:47:05
1.同步代码块:   反编译结果:    monitorenter : 每个对象有一个监视器锁(monitor)。当monitor被占用时就会处于锁定状态,线程执行monitorenter指令时尝试获取monitor的所有权,过程如下: 1、如果monitor的进入数为0,则该线程进入monitor,然后将进入数设置为1,该线程即为monitor的所有者。 2、如果线程已经占有该monitor,只是重新进入,则进入monitor的进入数加1. 3.如果其他线程已经占用了monitor,则该线程进入阻塞状态,直到monitor的进入数为0,再重新尝试获取monitor的所有权。 monitorexit:   执行monitorexit的线程必须是objectref所对应的monitor的所有者。   指令执行时,monitor的进入数减1,如果减1后进入数为0,那线程退出monitor,不再是这个monitor的所有者。其他被这个monitor阻塞的线程可以尝试去获取这个 monitor 的所有权。   Synchronized的语义底层是通过一个monitor的对象来完成,其实wait/notify等方法也依赖于monitor对象,这就是为什么只有在同步的块或者方法中才能调用wait/notify等方法,否则会抛出java.lang

Java 7 Calendar.getInstance, TimeZone.getTimeZone got synchronized and slow, any work arounds?

断了今生、忘了曾经 提交于 2019-12-06 07:28:06
问题 After upgrading my runtime to Java 7 I see incredible slowness... and my program is spending all of its time in the SimpleDateFormat constructor. As described in a great post here: http://coffeedriven.org/?p=83 the TimeZone code is now checking for the presence of an application context in the static synchronized method getDefaultInAppContext() . The problem for me is that it's Spring Batch file reader code that is creating a new SimpleDateFormat object for each line that it reads! Anyone got

Java面试之synchronized 和 static synchronized

妖精的绣舞 提交于 2019-12-06 06:21:58
​面试题: 答案: 不能 不能 不能 不能 能 正文 概述 通过分析这两个用法的分析,我们可以理解java中锁的概念。一个是实例锁(锁在某一个实例对象上,如果该类是单例,那么该锁也具有全局锁的概念),一个是全局锁(该锁针对的是类,无论实例多少个对象,那么线程都共享该锁)。实例锁对应的就是synchronized关键字,而类锁(全局锁)对应的就是static synchronized(或者是锁在该类的class或者classloader对象上)。 区别 synchronized关键字 synchronized作用是对类的当前实例(对象)加锁。可以使用synchronized关键字来标记一个方法或者代码块,当某个线程调用该对象的synchronized方法或者访问synchronized代码块时,这个线程便获得了该对象的锁,其他线程暂时无法访问这个方法,只有等待这个方法执行完毕或者代码块执行完毕,这个线程才会释放该对象的锁(Java 并发编程)。 synchronized代码块【synchronized(synObject)】使用起来比synchronized方法要灵活得多。因为也许一个方法中只有一部分代码只需要同步,如果此时对整个方法用synchronized进行同步,会影响程序执行效率。而使用synchronized代码块就可以避免这个问题(同步对象或类属性)

synchronized not synchronizing

爱⌒轻易说出口 提交于 2019-12-06 06:04:27
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 timeToMove_ to false"); timeToMove_ = false; Log.v("meh", "timeToMove_ is now false"); } } } } // Called by a

Synchronized reordering in java

牧云@^-^@ 提交于 2019-12-06 05:52:33
问题 It is known that JVM shouldn't reorder statements from withing synchronized block to outside of the synchronized block. Considering this, is JVM allowed to reorder assignment y = 7 to occur after the synchronized block in the following snippet? x = 5; y = 7; synchronized (this) { x = 6; } We know that variable assignment before the synchronized block can be reordered to occur inside the block. So the following should be valid reordering of the initial code: x = 5; synchronized (this) { x = 6;

锁如何使用?有什么注意事项?

空扰寡人 提交于 2019-12-06 05:25:28
Java 中常见的锁有 synchronized 可重入锁 java.util.concurrent.lock.ReentrantLock 可重复读写锁 java.util.concurrent.lock.ReentrantReadWriteLock synchronized 有 3种用法 修饰普通方法,执行方法代码,需要获取对象本身 this 的锁 package constxiong.concurrency.a18; import java.util.ArrayList; import java.util.List; /** * 测试 synchronized 普通方法 * @author ConstXiong * @date 2019-09-19 10:49:46 */ public class TestSynchronizedNormalMethod { private int count = 0; // private void add1000() { private synchronized void add1000() { //使用 synchronized 修饰 add100 方法,即可获得正确的值 30000 for (int i = 0; i <1000; i++) { count++; } } //启动 30 个线程,每个线程 对