Java Multi-Threading 6 (thread and communication)

Source: Internet
Author: User

Question 1:

Why Wait () and notify () are methods in the object class, not methods in the thread class?

A: Because the lock is an arbitrary object, in the object class, if the lock object is not arbitrary in the thread class.

Question 2:

Why Wait () and notify () must be used in synchronous code fast or in a synchronous function?

A: Because Wait () and notify () are used when there is a lock, there is a lock in the synchronization code block.

Question 3:

Why Wait () and notify () must be called by the lock object?

A: Wait () and notify () to set up the thread pool with the lock object as an identifier.

Add:

Wait (): If a thread executes the wait () method, the thread will go into a thread pool waiting for the lock object as an identifier .

Notify (): If a thread executes the Notify () method, the thread wakes up a waiting thread in the thread pool with the lock object as an identifier.

Notifyall (): Wakes all waiting threads in the thread pool.

Attention:

1. The lock object is the same only in the same thread pool, otherwise in a different pool.

2.wait () and notify () must be called with the lock object, or the error will be immediately.

Java Multi-Threading 6 (thread and communication)

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.