Can we call the Thread.sleep in Synchyronozed block?

Answer Posted / anmol mathpal

yes u can but be sure to use it under try-catch block.
Actually synchronized keyword is basically used for locking
purpose. let us consider an example:-

/*This is a synchronized block*/
Synchronized(this){
try{Thread.sleep(1000);}catch(InterruptedException e){}
//Remaining code
}
Now suppose there is s thread in a program say A
let us consider thread A enters the synchronized block
because of sleep method it'll take one second more than its
normal execution time.that's it. it will affect other
threads that are waiting for that particular resource.
without thread.sleep method

Is This Answer Correct ?    2 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What happens if main method is not static?

588


What is exception handling in java?

653


Why destructor is not used in java?

618


What are operators and its types?

656


Explain heap sort?

792






What is an immutable class? How to create an immutable class?

630


What is the difference between heap and stack memory?

637


what type of questions asked for barclays technologies pune please send urgent

5336


How to stop a thread in java? Explain about sleep () method in a thread?

684


What is thread safe java?

591


I want to re-reach and use an object once it has been garbage collected. Define how it’s possible?

673


What is the main use of generics in java?

604


How are the elements of a gridbaglayout organized?

644


What is a JAR file?

677


What is outofmemoryerror in java?

655