• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

synchronized methods and blocks worries

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*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);

}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic