synchronized-block

What is the purpose of passing parameter to synchronized block?

℡╲_俬逩灬. 提交于 2019-11-29 23:09:57
I know that When you synchronize a block of code, you specify which object's lock you want to use as the lock, so you could, for example, use some third-party object as the lock for this piece of code. That gives you the ability to have more than one lock for code synchronization within a single object. However, I don't understand the need of passing argument to the block. Because it doesn't matter whether I pass String's instance, Some random class's instance to the synchronized block as the synchronized block works perfectly irrespective of the parameter being passed to the block. So my

Synchronized block not working

*爱你&永不变心* 提交于 2019-11-29 09:28:09
This exercise is straight out of SCJP by Kathy Seirra and Bert Bates Synchronizing a Block of Code In this exercise we will attempt to synchronize a block of code. Within that block of code we will get the lock on an object, so that other threads cannot modify it while the block of code is executing. We will be creating three threads that will all attempt to manipulate the same object. Each thread will output a single letter 100 times, and then increment that letter by one. The object we will be using is StringBuffer. We could synchronize on a String object, but strings cannot be modified once

What is the purpose of passing parameter to synchronized block?

强颜欢笑 提交于 2019-11-28 20:32:00
问题 I know that When you synchronize a block of code, you specify which object's lock you want to use as the lock, so you could, for example, use some third-party object as the lock for this piece of code. That gives you the ability to have more than one lock for code synchronization within a single object. However, I don't understand the need of passing argument to the block. Because it doesn't matter whether I pass String's instance, Some random class's instance to the synchronized block as the

Rejecting class because it failed compile-time verification Android

烂漫一生 提交于 2019-11-28 12:09:12
One of my application suddenly fails on startup, with the following error message : java.lang.VerifyError: Rejecting class com.sample.BufferManagerImpl because it failed compile-time verification (declaration of 'com.sample.BufferManagerImpl' appears in /data/app/com.sample.myapp-1/base.apk) It only fails on devices using the ART virtual machine, but not on Dalvik The issue is due to having a synchronized block inside a try-catch block, for example : try { synchronized (mLock) { updateState(); } } catch (IllegalStateException e) { } Apparently this is not a good practice, but as soon as I

Synchronized block not working

删除回忆录丶 提交于 2019-11-28 03:10:07
问题 This exercise is straight out of SCJP by Kathy Seirra and Bert Bates Synchronizing a Block of Code In this exercise we will attempt to synchronize a block of code. Within that block of code we will get the lock on an object, so that other threads cannot modify it while the block of code is executing. We will be creating three threads that will all attempt to manipulate the same object. Each thread will output a single letter 100 times, and then increment that letter by one. The object we will

Rejecting class because it failed compile-time verification Android

一曲冷凌霜 提交于 2019-11-27 06:53:20
问题 One of my application suddenly fails on startup, with the following error message : java.lang.VerifyError: Rejecting class com.sample.BufferManagerImpl because it failed compile-time verification (declaration of 'com.sample.BufferManagerImpl' appears in /data/app/com.sample.myapp-1/base.apk) It only fails on devices using the ART virtual machine, but not on Dalvik 回答1: The issue is due to having a synchronized block inside a try-catch block, for example : try { synchronized (mLock) {

What is the difference between synchronized on lockObject and using this as the lock?

戏子无情 提交于 2019-11-26 18:33:50
I know the difference between synchronized method and synchronized block but I am not sure about the synchronized block part. Assuming I have this code class Test { private int x=0; private Object lockObject = new Object(); public void incBlock() { synchronized(lockObject) { x++; } System.out.println("x="+x); } public void incThis() { // same as synchronized method synchronized(this) { x++; } System.out.println("x="+x); } } In this case what is the difference between using lockObject and using this as the lock? It seems to be the same to me.. When you decide to use synchronized block, how do