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

Which container method is used to cause a container to be laid out and redisplayed in java programming?

604


How does a for loop work java?

547


what is mena by object block any what is the use of that

1774


How does multithreading take place on a computer with a single cpu in java programming?

536


What do you understand by soft reference?

586






What is meant by object?

583


Write a java program that prints all the values given at command-line.

558


What are desktop procedures?

562


How do you allocate memory to object?

520


What happens when you invoke a thread’s interrupt method while it is sleeping or waiting?

578


Is java an open source?

528


What is a variable declaration?

528


What is the static method?

571


What are the concepts of 'OOPS'?

602


Can we use synchronized block for primitives?

606