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
Which container method is used to cause a container to be laid out and redisplayed in java programming?
How does a for loop work java?
what is mena by object block any what is the use of that
How does multithreading take place on a computer with a single cpu in java programming?
What do you understand by soft reference?
What is meant by object?
Write a java program that prints all the values given at command-line.
What are desktop procedures?
How do you allocate memory to object?
What happens when you invoke a thread’s interrupt method while it is sleeping or waiting?
Is java an open source?
What is a variable declaration?
What is the static method?
What are the concepts of 'OOPS'?
Can we use synchronized block for primitives?