Reputation: 101
I am new to the multithreading programming. I am having a problem in understanding the behaviour of synchronized method access by multiple instance of the class.
In the below code am trying to implement wait and poll mechanism. Where am waiting for a response from one service for some time and if that service returned response within that time i will return back.
In this I have implemented two synchronized blocks. I can understand that two threads cant able to access the synchronized method at the same time. Here only I can get confused what if multiple instances of WaitAndPoll class created and invoked at the same time.
Will each instance execute one by one. If that so it will affect the performance badly in that case can anyone advise how to simplify this ?
public class WaitAndPoll {
Model model;
OSBService osbService;
WaitAndPoll(Model model, OSBService th1){
this.model = model;
this.osbService=th1;
}
// Prints a string and waits for consume()
public void waitingForOSBResponse()throws InterruptedException
{
synchronized(this)
{
System.out.println("waitingForOSBResponse thread running "+this.model.str);
this.osbService.start();
wait();
if(this.model.str==null) { // checking the response is still null if so calling java function
this.osbService.interrupt(); //This will interupt osb service thread
System.out.println(" Calling the java function");
}else{
System.out.println(" Response successfully returned from the OSBService :: "+this.model.str);
}
}
}
//Polling for every 1 second
public void pollingOSBResponse()throws InterruptedException
{
Thread.sleep(200);
synchronized(this)
{
int count=0;
while(this.model.str == null && count<3){
wait(1000);
System.out.println("wating for the modification");
++count;
}
System.out.println("Polling completed");
notify();
}
}
}
import java.util.Date;
public class OSBService extends Thread{
Model model;
OSBService(Model model){
this.model= model;
}
public void run(){
System.out.println("calling the osb webservice:: "+this.model.str);
try {
Thread.sleep(5000); //Simulating the wating period for the response
this.model.str="modified";
System.out.println("called the osb webservice:: "+this.model.str);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.err.println("OSB service interrupted because of longer time for response ::: "+this.model.str+" :: "+new Date().toString());
}catch (Exception e){
e.printStackTrace();
}
}
}
public class Main
{
public static void main(String[] args) throws InterruptedException
{
Model model = new Model();
model.str=null;
OSBService osbService = new OSBService(model);
final WaitAndPoll waitAndPoll = new WaitAndPoll(model,osbService);
//Calling the OSB service and waiting for its response
Thread t1 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
waitAndPoll.waitingForOSBResponse();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
//Polling whether the osb reponse received or not
Thread t2 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
waitAndPoll.pollingOSBResponse();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
}
Upvotes: 1
Views: 48
Reputation: 1436
If multiple instance are created, then the methods will be try to acquire mutex lock in synchronized block on different instances of the WaitAndPoll class. If you are passing different instances to the threads then there is no guarantee which thead will execute first the synchronized block.
Upvotes: 1