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
Is singleton thread safe in java?
What is main difference between variable and constant?
Explain the difference between the public, private, final, protected, and default modifiers?
What is array pointers ?
Explain the scope of a variable.
Can we sort set in java?
What are the types of literals?
Difference between arraylist and vector.
What is the difference between and ?
Mention some interfaces implemented by linked list in java.
Explain the selection sort algorithm and state its time complexity?
Explain OOPs concept.
What is singleton class example?
What are the types of methods in java?
What is the purpose of static keyword in java?