Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Why two threads accessing synchronized method at the same time

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • 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: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and synchronize the getInstance() method!
 
Parmeet Singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanking you ..
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic