answersLogoWhite

0

Thread.sleep(long milliseconds): The thread's sleep method sends the current thread into Non-Runnable state for the specified amount of time. But, this doesn't cause the thread to loose ownership of the acquired monitors. So, if the current thread is into a synchronized block/method, then no other thread will be able to enter that block/method.

The Sleep method throws 'InterruptedException' if another thread interrupts it.

There is another variant of the 'sleep()' method where it accepts two arguments - one, long milliseconds, and second, int nanoseconds. Simply put, this method causes the current thread to sleep for the specified number of milliseconds plus the specified number of nanoseconds. The second argument 'int nanoseconds' can acquire a value of the range 0-999999.

wait() method: The wait() method also sends the current thread into Non-Runnable state like the sleep() method. But, the difference between the two is that in case of 'wait()' the locks are released before going into Non-Runnable state, so that any other thread that is waiting on the object can use it (Unlike the Sleep method - This is the big difference) Another apparent difference is that 'wait()' is an instance method, while sleep() is a static method. The method 'wait()' should be called for an object only when the current thread has already acquired lock for that object. This causes the current thread to wait either another thread invokes the 'notify()' method or the 'notifyAll()' method for this object, or a specified amount of time has elapsed and after that the thread starts participating in thread scheduling process to acquire the monitor of the object to proceed further.

There are three variants of this method in the 'Object' class:-

public final void wait(long timeout)

public final void wait(long timeout, int nanoseconds)

public final void wait()

All the three methods throw InterruptedException & IllegalMonitorStateException. The first two may also throw IllegalArgumentException.

The wait() method causes the current thread to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. After the execution of this method invocation, the thread becomes disabled for any scheduling purposes and lies dormant until one of the following things happen:-

* Any other thread invokes 'notify()' method this object and the thread under consideration is arbitrarily chosen as the thread to be awakened. * Any other thread invokes 'notifyAll()' for this object. * Any other thread interrupts the thread under consideration. * The specified amount of time has elapsed (in case first two variants of wait() are used) After any of the four above mentioned events happens, the thread is removed from the wait set for this object and re-enabled for thread scheduling. It'll compete for the rights to synchronize on the object in an usual manner and it'll keep doing this until it acquires control on the object and gains all the synchronization claims on the object, which it had acquired at the time of 'wait()' invocation. This means that after the return from wait() method, the synchronization state of object and of the thread will be exactly the same as it was before the wait() invocation.

User Avatar

Wiki User

16y ago

What else can I help you with?

Related Questions

What is the difference between a discount yield and a bond equivalent yield?

Chamika & n


What is the difference between YTD Return vs Yield?

yield vs ytd


What is the difference between crop yield and crop production?

yield is per area, production is total (at least according to FAO)


What is the difference between a merge and a yield - relative to traffic laws?

The Difference between a merge and a yield is, when your merging, you are entering oncoming traffic with out stopping, and yielding s letting the traffic pass you and then going when the coast is clear.


What is the difference in dividend yield between FXAIX and VOO?

The difference in dividend yield between FXAIX and VOO is the percentage by which the annual dividend payments of FXAIX exceed or fall short of the annual dividend payments of VOO.


What is the difference between y42 and a 234 wpb pipe fittings?

yield strength


What is the difference between the predicted and actual yield in a chemical reaction?

The actual yield of a reaction product is always less than the yield from the chemical equation. This is because of error.


What is the difference between brother tn360 and tn450 toner cartridge?

The difference is that the brother tn330 is a low yield cartridge and the tn360 is a high yield cartridge.


What is the difference between a peroxide and a dioxide?

Because peroxide will yield H202 upon reaction with acid while Dioxide will not yield H202 with acid


What is the difference between nuclear weapons and normal weapons?

energy release aka yield


What is the difference between yield and coupon rate?

The difference between the coupon rate and the required return of a bond is dependent upon the type of bond. Junk bonds will have the biggest difference between its return and the coupon rate.


What is the difference between actual yield and theoretical yield?

Theoretical= calculated