Some knowledge points about the current thread in Java

Source: Internet
Author: User

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:

  1. Where synchronized is available, wait and policy may not be available.
  2. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.