ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
Google
 
Categories  >>  Software  >>  Core Java  >>  Java J2EE  >>  Java Related
 
 


 

 
 Core Java interview questions  Core Java Interview Questions
 Advanced Java interview questions  Advanced Java Interview Questions
 Swing interview questions  Swing Interview Questions
 EJB interview questions  EJB Interview Questions
 Servlets interview questions  Servlets Interview Questions
 Struts interview questions  Struts Interview Questions
 JDBC interview questions  JDBC Interview Questions
 JMS interview questions  JMS Interview Questions
 SunOne interview questions  SunOne Interview Questions
 J2EE interview questions  J2EE Interview Questions
 Weblogic interview questions  Weblogic Interview Questions
 Websphere interview questions  Websphere Interview Questions
 Java Networking interview questions  Java Networking Interview Questions
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions
Question
what is the difference between sleep() and Wait()?
 Question Submitted By :: Rdharmendra
I also faced this Question!!     Rank Answer Posted By  
 
  Re: what is the difference between sleep() and Wait()?
Answer
# 1
Thread.sleep() sends the current thread into the "Not 
Runnable" state for some amount of time. The thread keeps 
the monitors it has aquired -- i.e. if the thread is 
currently in a synchronized block or method no other thread 
can enter this block or method. If another thread calls 
t.interrupt() it will wake up the sleeping thread. 
Note that sleep is a static method, which means that it 
always affects the current thread (the one that is 
executing the sleep method). A common mistake is to call 
t.sleep() where t is a different thread; even then, it is 
the current thread that will sleep, not the t thread. 

object.wait() sends the current thread into the "Not 
Runnable" state, like sleep(), but with a twist. Wait is 
called on a object, not a thread; we call this object 
the "lock object." Before lock.wait() is called, the 
current thread must synchronize on the lock object; wait() 
then releases this lock, and adds the thread to the "wait 
list" associated with the lock. Later, another thread can 
synchronize on the same lock object and call lock.notify(). 
This wakes up the original, waiting thread. Basically, wait
()/notify() is like sleep()/interrupt(), only the active 
thread does not need a direct pointer to the sleeping 
thread, but only to the shared lock object.
 
Is This Answer Correct ?    15 Yes 3 No
Agrawalamit166
[IBM]
 
  Re: what is the difference between sleep() and Wait()?
Answer
# 2
The main difference is if we need the thread wants to block
particular period of time we just choose sleep().
and when the thread called wait() means it will blocked
untill we will call notify(),notifyAll();
 
Is This Answer Correct ?    8 Yes 2 No
Deepak Divvela
 
 
 
  Re: what is the difference between sleep() and Wait()?
Answer
# 3
Wait() : This function takes parameters as milliseconds and
puts the thread in wait state for the desired time of the
programmer after time passes the execution starts again.

Sleep() : This function is also used for same purpose using
his function by java you can put a thread in sleep state
.sleep does not contains any parameters so the thread will
not be automatically start execution It needs a wake up
signal again which can be Notify().or other function are
also provided by java.

So the main difference in Wait() and sleep() is wait takes
time parameter and wait for specific time only and sleep
throws a thread in sleep mode for unspecified time.
 
Is This Answer Correct ?    1 Yes 11 No
Modi[achir Communication Pvt.
 
  Re: what is the difference between sleep() and Wait()?
Answer
# 4
Sleep():

Pause  for  NUMBER  seconds.  SUFFIX may be 's' for seconds 
(the default), 'm' for minutes, 'h' for hours or 'd' for 
days.  Unlike most implementations that require NUMBER be 
an integer, here NUMBER may be an arbitrary floating point 
number.


Wait():

The  wait  function  suspends execution of the current 
process until a child has exited, or until a signal is 
delivered whose action  is  to terminate  the  current 
process or to call a signal handling function. If a child 
has already exited by the time of  the  call (a  so-
called "zombie" process),  the  function  returns  
immediately. Any system resources used by the child are 
freed.
 
Is This Answer Correct ?    2 Yes 2 No
Vijay
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
How multi processing is achieved in JAVA? BMC4
If you are reviewing the code of your team members, what points will you look at, assuming the performance of the application is not so great KPIT2
What is user defined exception?  4
what is the difference between abstract class and Interface?where we can use it in realtime projects? Logica-CMG10
What is meant by event handling in SAX parser? Ness-Technologies1
When garbage collector invokes object?s finalize() method?  3
what are depricated methods in threads and explain the lifecycle methods  5
how to transactions(Bank transactions) in business process in ejb? Photon1
What Method and class used for Connection pooling ? Wipro4
what is main purpose of abstract class?  2
What we have to do, when we don't want to implement all methods of an interface?  2
How to call static method? Epoch3
Difference between abstract class and Interfaces? Sai-Softech5
Is Java is 100% pure OOPS? Explain?  15
what is unreachable code problem in multiple catch statements  2
how to compile jsp? Logica-CMG3
How the elements are organized in BorderLayout?  4
What is meant by throwing an Exception? Accenture4
What are Access Specifiers and Access Modifiers. TCS4
what is bytecode? watz the difference between machine code and bytecode? Oracle4
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com