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 class and its types?

996


What will happen if static modifier is removed from the signature of the main method?

930


What do you mean by collectors in java 8?

905


How many threads can java run?

1079


What if the main() method is declared as private? What happens when the static modifier is removed from the signature of the main() method?

1164


How can you read content from file in java?

977


How java uses the string and stringbuffer classes?

1020


What does singleton class mean?

932


Why java applets are more useful for intranets as compared to internet?

1033


Why java is secure? Explain.

1000


what are the methods in object?

1114


What is the difference between private & public & friendly classes?

1039


what state does a thread enter when it terminates its processing? : Java thread

1010


Can we overload the constructors?

947


How do you declare an empty string?

1023