• 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

Why two threads accessing synchronized method at the same time

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a below given programme, when i run executer class, it makes two NoWai threads and these thread gets the reference of Workspace class.
Workspace class has a synchronized method name doDa(),

My question: why and how two threads are able to access synchronized method doDa() at the same time.

We can confirm this by checking the output i.e.

Message: WorkPlace@923e30 first
Thread name: Thread-1

Message: WorkPlace@923e30 first
Thread name: Thread-0

Message: WorkPlace@923e30 second
Thread name: Thread-1

Message: WorkPlace@923e30 second
Thread name: Thread-0

it shows two threads enter the synchronized method at teh same time.


class NoWai extends Thread{
public NoWai(){
start();
}

public void run(){
WorkPlace.getInstance().doDa();
}
}

class WorkPlace{
private Object lock = new Object();
private WorkPlace(){
}

static WorkPlace work = null;

public static WorkPlace getInstance(){
if(work == null){
work = new WorkPlace();
}
return work;
}


public synchronized void doDa(){
TUtil.print(this.getInstance() + " first");

TUtil.print(this.getInstance() + " second");
}
}


class Executer {
public static void main(String[] args) throws Exception{
for(int i =0 ; i < 2; i++){
new NoWai();
}
}
}

class TUtil{
static void print(String message){
System.out.println("Message: "+message +
"\nThread name: "+Thread.currentThread().getName() + "\n");
}
}
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you may have a race condition in your first/second call to the getInstance() method -- possibly giving you two different objects.

To be able to tell if this is happening, remove the getInstance() call in the call to the print() method. This is not needed anyway, because the "this" object is that instance, and the extra call to getInstance() will just hide the race condition.

Henry
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and synchronize the getInstance() method!
 
Parmeet Singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanking you ..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic