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
When a thread is executing synchronized methods , then is it possible to execute other synchronized methods simultaneously by other threads?
Explain importance of inheritance in java?
What is class variable java?
How we can execute any code even before main method?
What is a ?
What is string [] args?
Can we change the scope of the overridden method in the subclass?
When should you make a function static?
Explain serialization and deserialization in java?
What happens when you add a double value to a string?
What is a qms manual?
whatis Home interface and Remoteinterface? with example?
Write a program in java to find the maximum and minimum value node from a circular linked list.
What does a method signature consist of?
What does sprintf return?