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?

Answers were Sorted based on User's Feedback



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

Answer / jitender arora

By using a flag to determine that the method is in use by a
running thread.

Class A{

private boolean inUse = false;

public methodA(){
if(!inUse){
inUse = true;
...
...
...
inUse = false;
}
}

}

Is This Answer Correct ?    18 Yes 12 No

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

Answer / isak

Use ReentrantLock which belongs to java.util.concurrent.locks.ReentrantLock package
Below is the method printCount() which has synchronised without using synchronised.


Lock lock = new ReentrantLock();

public void printCount(String threadName){
lock.lock();
try {
for(int i = 5; i > 0; i--) {
System.out.println("Counter --- " + i +" With thread : "+threadName);
}
} catch (Exception e) {
System.out.println("Thread interrupted.");
}finally{
lock.unlock();
}
}

Is This Answer Correct ?    3 Yes 0 No

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

Answer / rajib

by implementing SingleThreadModel interface.

Is This Answer Correct ?    4 Yes 2 No

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

Answer / aravinda reddy

Oneway of doing By making all the variables and methods in
the class as final.

Is This Answer Correct ?    3 Yes 3 No

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

Answer / venkat

Hi Jitender,

Here the scnroniziation is happening in methodA(){}
But the same thread(Thread-0) is executing even second time
also. Could you please clarify the same.

Here is the output.

Constructor..
Thread started..Thread-0
processing....Thread-0
Thread started..Thread-1
complete..Thread-0
processing....Thread-0
complete..Thread-0

Regards,
Venkat

Is This Answer Correct ?    0 Yes 1 No

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

Answer / dandhar

Make the "inUse" variable to static.

private static boolean inUse = false;

Is This Answer Correct ?    2 Yes 3 No

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

Answer / 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

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

Answer / 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

More Core Java Interview Questions

Why stringbuffer is faster than string?

0 Answers  


printstream class method println() is calling using System class and its static object out .how it is explain any one in detail with example ?

1 Answers   TCS,


what is the difference between static class and singleton class? can we create static class?

2 Answers   L&T, Octazen, Vamsi Labs,


Difference between flush() and commit() in hibernate?

2 Answers   Bally Technologies,


Hi Anyone know the model / questions of the Federal bank sample questions for the post of Specialist Officers - Programmers. Please post if anyone have..

0 Answers  


How do you avoid global variables?

0 Answers  


what is singleton class in java?

0 Answers   Cap Gemini,


What is linkedlist in java?

0 Answers  


Difference between object and reference?

0 Answers  


Java violates few objected oriented concepts. Can you explain in what way?

3 Answers   Ness Technologies,


Difference between a Scrollbar and a ScrollPane?

1 Answers  


What are the Main functions of Java?

0 Answers   Akamai Technologies,


Categories