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 is meant by method overriding?

538


What is return used for in java?

515


Define how does a try statement determine which catch clause should be used to handle an exception?

578


What is the difference between multiple processes and multiple threads?

570


What are the advantages of compiled language?

570






What is the synchronized method modifier?

583


Objects or references which of them gets garbage collected?

529


How many bits are used to represent unicode, ascii, utf-16, and utf-8 characters?

579


why using interface interface ?

1500


Can you use this() and super() both in a constructor?

520


Explain different forms of polymorphism?

648


How do you sort a set in java?

513


What is a dot notation?

544


What is the difference between stringbuffer and stringbuilder class?

560


What is procedure writing?

507