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 / jitender arora

Corrected my previous answer:

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);
t1.start();

Thread t2 = new Thread(a);
t2.start();


}

public void run(){
System.out.println("Thread
started.."+Thread.currentThread().getName());

Thread.currentThread().getName();
methodA();
}
private boolean inUse = false;

public void methodA(){
while(!inUse){

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;
}

}

Is This Answer Correct ?    5 Yes 9 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

When a thread is executing synchronized methods , then is it possible to execute other synchronized methods simultaneously by other threads?

997


Explain importance of inheritance in java?

963


What is class variable java?

954


How we can execute any code even before main method?

1074


What is a ?

1152


What is string [] args?

956


Can we change the scope of the overridden method in the subclass?

1312


When should you make a function static?

925


Explain serialization and deserialization in java?

938


What happens when you add a double value to a string?

933


What is a qms manual?

936


whatis Home interface and Remoteinterface? with example?

1943


Write a program in java to find the maximum and minimum value node from a circular linked list.

1005


What does a method signature consist of?

923


What does sprintf return?

1007