wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized methods and blocks worries Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized methods and blocks worries" Watch "synchronized methods and blocks worries" New topic
Author

synchronized methods and blocks worries

Mahmoud Metwaly
Greenhorn

Joined: Jul 16, 2008
Posts: 14
hi all,

I am not sure I am getting the idea of how synchronized static methods. synchronized blocks and synchronized static blocks works correctly

For synchronized static methods, as far as I understand every class has its own lock through its corresponding instance of java.lang.Class...which means that no matter how many objects do exist for the class that do have the synchhronized static methods, only ONE lock can be done at a time on the synchronized static methods of the class through its corresponding instance of java.lang.Class

Now what I am confused about in the classes which contains synchronized static methods or to be more specific in classes that contain synchronized static methods, synchronized methods, synchronized static blocks AND synchronized blocks in ONE class...what will happen for example if a thread gained lock on the synchronized static method of this class...can other threads access the synchronized methods, synchronized static blocks, synchronized blocks of this same class?


For instance and static blocks, I really don't understand how their blocking mechanism work, also the object on which the lock will happen so I will appreciate it so much if someone can please help me by explaining those two parts as well

Thanks in advance for your time and efforts


Mahmoud Metwally<br />Preparing for SCJP...<br /> <br />"Try not to become a man of success but a man of value" - Albert Einstein
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
It`s not necessary that lock could be on own lock through its corresponding instance of java.lang.Class...

refer flowing example.

public class Sync {


public static void main(String[] args) {
}

//case 1
public void syncBolckWithClassLock(){
synchronized (Sync.class) {
}
}

//case 2
public void syncBolckWithObjectLock(){
synchronized (this) {
}
}
//case 3 - same behaviour like case 2.
public synchronized void syncMethodWithObjectLock(){}
}


each synchronization having one monitor object, in synchronize block you will explicitly define that monitor object.

But in synchronize method it`s default 'this'.

now other object can get lock on synchronize part or not it`s depend on you monitor object.

case 1:- monitor object is Sync.class so onces any Thread enter in this part other one can`t acquire lock.

case 2 and 3 :- but in if you call method form different object this is not locked you will acquire the lock.

try to Run following 3 example one by one.. and observer output




kaushik Vira
-------------------------------------
SCJP, Preparing SCWCD..
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
/*case 1*/

public class Sync {


static Sync s1 = new Sync();
static Sync s2 = new Sync();

//case 1
public void syncBolckWithClassLock() throws Exception{
synchronized (Sync.class) {
System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName());
Thread.currentThread().sleep(Long.MAX_VALUE);
}
}

//case 2
public void syncBolckWithObjectLock() throws Exception{
synchronized (this) {
System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName());
Thread.currentThread().sleep(Long.MAX_VALUE);
}
}
//case 3 - same behaviour like case 2.
public synchronized void syncMethodWithObjectLock() throws Exception{
System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName());
Thread.currentThread().sleep(Long.MAX_VALUE);
}



public static void main(String [] args) throws Exception{
Thread.currentThread().setName("Main");



Thread t1 = new Thread(){
@Override
public void run(){
try{
s1.syncBolckWithClassLock();
}catch(Exception e){
e.printStackTrace();
}
}
};
t1.setName("t1");
t1.start();
Thread t2 = new Thread(){
@Override
public void run(){
try{
s2.syncBolckWithClassLock();
}catch(Exception e){
e.printStackTrace();
}
}
};
t2.setName("t2");
t2.start();

Thread.currentThread().sleep(Long.MAX_VALUE);

}
}
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
/*case 2*/

public class Sync {


static Sync s1 = new Sync();
static Sync s2 = new Sync();

//case 1
public void syncBolckWithClassLock() throws Exception{
synchronized (Sync.class) {
System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName());
Thread.currentThread().sleep(Long.MAX_VALUE);
}
}

//case 2
public void syncBolckWithObjectLock() throws Exception{
synchronized (this) {
System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName());
Thread.currentThread().sleep(Long.MAX_VALUE);
}
}
//case 3 - same behaviour like case 2.
public synchronized void syncMethodWithObjectLock() throws Exception{
System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName());
Thread.currentThread().sleep(Long.MAX_VALUE);
}



public static void main(String [] args) throws Exception{
Thread.currentThread().setName("Main");

Thread t1 = new Thread(){
@Override
public void run(){
try{
s1.syncBolckWithObjectLock();
}catch(Exception e){
e.printStackTrace();
}
}
};
t1.setName("t1");
t1.start();
Thread t2 = new Thread(){
@Override
public void run(){
try{
s2.syncBolckWithObjectLock();
}catch(Exception e){
e.printStackTrace();
}
}
};
t2.setName("t2");
t2.start();
Thread.currentThread().sleep(Long.MAX_VALUE);

}
}
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
/*case 3*/

public class Sync {


static Sync s1 = new Sync();
static Sync s2 = new Sync();

//case 1
public void syncBolckWithClassLock() throws Exception{
synchronized (Sync.class) {
System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName());
Thread.currentThread().sleep(Long.MAX_VALUE);
}
}

//case 2
public void syncBolckWithObjectLock() throws Exception{
synchronized (this) {
System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName());
Thread.currentThread().sleep(Long.MAX_VALUE);
}
}
//case 3 - same behaviour like case 2.
public synchronized void syncMethodWithObjectLock() throws Exception{
System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName());
Thread.currentThread().sleep(Long.MAX_VALUE);
}



public static void main(String [] args) throws Exception{
Thread.currentThread().setName("Main");

Thread t1 = new Thread(){
@Override
public void run(){
try{
s1.syncMethodWithObjectLock();
}catch(Exception e){
e.printStackTrace();
}
}
};
t1.setName("t1");
t1.start();
Thread t2 = new Thread(){
@Override
public void run(){
try{
s2.syncMethodWithObjectLock();
}catch(Exception e){
e.printStackTrace();
}
}
};
t2.setName("t2");
t2.start();


Thread.currentThread().sleep(Long.MAX_VALUE);

}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized methods and blocks worries
 
Similar Threads
regarding static method
chapter 9 self test Q.15
Thread Locking a Object - Doubt
What is meaning of a static method to synchronized???
questions