You are here:

Java/synchronized block


hi sir!
The general form of synchronized block is
class table
void printTable(int n)
Here obj means object of table class or object of any class.
If we write 'this' in place of obj ,then an object of table class canít access the synchronized block from two different threads.
1)Is it possible to write name of object other than table class in place of obj?
2)If write other objectís name other than table class in place of obj,what it means?
3)Then which object canít access the synchronized block simultaneously?

1. It is possible to put any object in a synchronized block.

When a thread enters a synchronization block, it is said to have a lock on that object. That lock is released once the block is left.

What happens is if a thread has a lock on an object, and another thread tries to lock that object by entering a synchronization block, it will wait until the lock is free.

The important things to note is that the synchronization blocks can be in different locations, but if they are on the same object, then they will block each other on different threads. Also note that a thread with a lock on an object doesn't prevent it from entering other synchronized blocks on the same object. Also, a thread needs to have a lock on an object in order to use wait() or notify() on the object. When a thread uses wait, it releases the lock on that object, then reacquires it when it continues.


All Answers

Answers by Expert:

Ask Experts


Artemus Harper


I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.


I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Washington State University

MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2017 All rights reserved.