Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

How to make a method thread safe without using synchronized
keyword?

Answer Posted / praveen t chand

hi

this is the correct answer for this question



public class A implements Runnable {

/**
* @author jeetendra.arora
* @param args
*/

A(){

System.out.println("Constructor..");
}
public static void main(String[] args) {

A a = new A();


Thread t1 = new Thread(a,"a thead");
t1.start();

Thread t2 = new Thread(a,"b thead");
t2.start();


}
private boolean inUse = false;
private boolean f= false;
public void run(){
System.out.println("Thread
started.."+Thread.currentThread().getName());

while(!f)
if(!inUse){
methodA();
f= true;
}
}


public void methodA(){

inUse = true;

System.out.println("processing...."+Thread.currentThread().getName());

try{
Thread.currentThread().sleep(3000);
}
catch (Exception e){
System.out.println("Exp");
}



System.out.println("complete.."+Thread.currentThread().getName());
inUse = false;

}

}

regards
praveen

Is This Answer Correct ?    0 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between preemptive scheduling and time slicing?

1005


How do you declare a destructor in java?

931


Howto get an object that will perform date & time calculations then format it for output in some different locales with different date style.can ne1 tel me the answer of this question.pls

1852


Can an interface have a constructor?

900


What is the public method modifier?

954


What is style and indentation?

955


What are the legal parameters?

943


why Java does not support multiple inheritances?

1116


Java is Pass by Value or Pass by Reference?

997


How does map works in java?

943


What is boolean example?

895


What is a bubble sort in java?

901


Does string is thread-safe in java?

1023


Can a class be private in java?

945


What is substring 1 in java?

1063