Questions tagged [synchronized-block]
85 questions
4
votes
4 answers
Synchronizing on an object and changing the reference
Let's say I have an object as follows:
Map m = new HashMap<>();
Then I synchronize on this object as follows and change its reference:
synchronize(m){
m = new HashMap<>();
}
With this code, what happens to the lock on m? Is it…

gmemon
- 2,573
- 5
- 32
- 37
4
votes
1 answer
Synchronized block: are variables "inside" other variables updated
Sorry for the non-technical title, but I think it summarizes my question well. If I interpret what I've read correctly, the synchronized block (apart from other consequences) will make all variables updated to/from the main memory (even those which…

Thomas Calc
- 2,994
- 3
- 30
- 56
3
votes
1 answer
Design pattern: How to create synchronized method or block in Singleton pattern in Java?
I am implementing a backend service using Java. I chose to apply the Singleton pattern because there should be only one service running. However, this service is also a Socket.IO client thus there must be some kind of event being triggered when…

loudking
- 115
- 4
3
votes
4 answers
Using 'this' versus another object as lock in synchronized block with wait and notify
I have two blocks of code, one waits for the other to notify it.
synchronized(this) {
wait();
}
and
while(condition) {
//do stuff
synchronized(this) {
notify();
}
}
Weirdly enough that didn't wait for the notify while this…

Chad
- 2,041
- 6
- 25
- 39
3
votes
4 answers
Why are WebMethods blocked when threads are synchronized?
Please see my code example of a JAX-WS Webservice:
@WebService
public class ClassA {
@WebMethod
public synchronized void doSomething() {
new Thread(new Runnable() { // Thread X
@Override
public void run() {
…

Zeemee
- 10,486
- 14
- 51
- 81
2
votes
3 answers
Inaccessible variable declared in synchronized block - Java
This piece of code will not compile:
synchronized( obj ) {
Object a = new Object()
}
System.out.println( a.toString() );
Yet I don't know why.. My understanding was that a synchronized block was always eventually executed, so I…

Antiz
- 1,424
- 1
- 13
- 20
2
votes
4 answers
How to correctly use synchronized?
This piece of code:
synchronized (mList) {
if (mList.size() != 0) {
int s = mList.size() - 1;
for (int i = s; i > 0; i -= OFFSET) {
mList.get(i).doDraw(canv);
}
getHead().drawHead(canv);
…

SBoss
- 8,845
- 7
- 28
- 44
2
votes
3 answers
Do we need to synchronize writes if we are synchronizing reads?
I have few doubts about synchronized blocks.
Before my questions I would like to share the answers from another related post Link for Answer to related question. I quote Peter Lawrey from the same answer.
synchronized ensures you have a…

nits.kk
- 5,204
- 4
- 33
- 55
2
votes
2 answers
Object lock in Java
I am trying to understand "synchronized block" in Java. I have written very basic code to see what happens if I lock and change the object in thread_1 and access to it from another thread_2 (race condition) via another method. But I am in trouble to…
2
votes
2 answers
Java synchronize block on same object in different methods
I am trying to understand the concept of synchronized blocks in java.
As of the documents that I have read, I understood that if we acquire
a lock ( synchronized block using an instance variable ) then we
cannot acquire a synchronized lock on same…

Rock
- 69
- 10
2
votes
0 answers
Nested synchronized blocks in Objective-C
I read code somewhere in which multiple identical synchronized blocks are used, like this:
@implementation Test {
NSObject var;
}
- (void)method1 {
@synchronized(self) {
@synchronized(var) {
...
}
}
}
- (void)method2 {
…

Boon
- 40,656
- 60
- 209
- 315
2
votes
3 answers
Is not this code violating mutual exclusion in critical section?
I am new to Java and trying to understand concurrency in Java. While exploring I came across this code on quite a popular page on Java concurrency:
public class CrawledSites {
private List crawledSites = new ArrayList();
private…

randomcompiler
- 309
- 2
- 14
2
votes
3 answers
Why do we write Synchronized(ClassName.class)
I have a question in singleton pattern.
In singleton pattern we write
synchronized(ClassName.class){
// other code goes here
}
What is the purpose of writing ClassName.class?

Rajan
- 124
- 2
- 12
2
votes
1 answer
What effect does the monitor object have in synchronized block?
After hours of reading i am still struggling to understand what the monitor object exactly does.
A demo to show what i mean:
public class Demo {
public static Bathroom bathroom = new Bathroom();
public static Kitchen kitchen = new…

Kaarel Purde
- 1,255
- 4
- 18
- 38
2
votes
4 answers
Any chance of deadlock with only one sync point?
I have two running threads calling few methods (5 or 6) where I specified synchronized block inside and use only one object to lock it. Is there any chance of deadlock with having only one sync point? So far, I haven't seen such a situation, but…

MartinC
- 546
- 11
- 26