Some common methods of the current thread:
Wait (), wait (long), Y (), policyall () and other methods are the instance methods of the current class,
- Wait () is used to release the lock for the thread holding the object lock;
- Wait (long) is used to obtain the lock again after the thread holding the object lock is released for long (milliseconds). Wait () is equivalent to wait (0;
- Y () is to wake up a thread waiting for the lock of this object. If there are more than one waiting thread, the wake-up thread is determined by JVM;
- Policyall is used to wake up all threads waiting for the lock of this object. we should first use the yyall () method, because it is easier for the JVM to find the most appropriate thread to wake up than to wake up a thread.
The preceding method can only be used in the current thread. Otherwise, the runtime error java. Lang. illegalmonitorstateexception: current thread not owner is reported.
The relationship between synchronized and wait () and notify:
- Where synchronized is available, wait and policy may not be available.
- With wait, synchronized must be used in zooy. this is because wait and notify are not thread classes, but methods that every object has. Moreover, these two methods are related to the object lock. Where there is a lock, there must be synchronized.
In addition, note that if you want to use the notify and wait methods together, you must first call notify and then call wait, this thread is no longer the current thread.