Compare the sleep() and wait() methods in Java
Compare the sleep() and wait() methods in Java
In Java, both sleep() and wait() methods are used to pause the execution of a thread, but they serve different purposes and are used in different contexts.
sleep() Method:sleep() is a static method of the Thread class.sleep() can be called from any context and is not dependent on the object's monitor.sleep() has two overloaded versions: sleep(long millis) and sleep(long millis, int nanos), both of which can throw InterruptedException.InterruptedException.sleep() always affects the current executing thread, regardless of which thread object calls it.wait() Method:wait() is a non-static method and is part of the Object class.notify() or notifyAll() on the same object or until the optional timeout expires.Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.