java multithreading basis (b) - sleep (), wait, () yield () and join () method

1. SLEEP () method

Currently executing thread to suspend execution within a specified time, but does not release the "lock flag." Not recommended.

sleep () makes the current thread into the blocked state, it will not be executed within a specified time.

2. the wait () method

Before notify or notifyAll method other thread calls the object, leading to the current thread to wait. Thread can release its possession "lock flag" so that other threads have the opportunity to seize the lock.

The current thread must have a current object lock. If the current thread is not the owner of this lock, IllegalMonitorStateException will throw an exception.

Wake the current object lock waiting threads using notify or notifyAll method, you must have the same object lock, otherwise it will throw an exception IllegalMonitorStateException.

waite () and notify () function must be called in synchronized or synchronized block in. If you call a function in non-synchronized or non-synchronized block, although able to compile, but at runtime IllegalMonitorStateException the exception occurs.

3. yield method 

Suspend the currently executing thread object.

yield () Causes the current thread just returned to an executable state, execution yield () thread is likely to be executed immediately after entering into an executable state.

yield () can only make the thread priority or with higher priority have a chance to execute. 

4. the Join Method

Waits for this thread.

Call join method of waiting for the end of the thread, and then continue. Such as: t.join (); // t wait for the main thread running end, without sentence, main is completed will be executed, leading to unpredictable results.

 

Updated on 2019.7.24

Guess you like

Origin www.cnblogs.com/bjm1/p/11235916.html