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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is singleton a bad practice?

1047


What is externalizable?

1063


Explain the difference between protected and default access.

999


Is java owned by oracle?

1018


Can we have multiple classes in single file ?

1070


What exceptions occur during serialization?

1088


What are dot operator queries?

1106


what is the messsage u r going to get from an objectoriented programing?

2104


Does anyone still use java?

1117


What does indexof mean?

1027


What is the exact difference in between unicast and multicast object? Where we will use?

999


Is c better than java?

962


Print Vertical traversal of a Binary Tree.

1017


What does 3 dots mean in java?

989


What are latest features introduced with java 8?

1089