Java Nineth Day

Source: Internet
Author: User

The first way to create a thread: Inherit the Thread class

Second way to create a thread: implementing the Runnable Interface

1. Define the class implementation runnable interface.

2. Overwrite the Run method in the interface to encapsulate the thread's task code in the Run method.

3. Create a thread object from the thread class and pass the subclass object of the Runnable interface as a parameter to the constructor in the thread class.

Why? Because the thread's tasks are encapsulated in the Run method of the Runnable interface subclass object. Therefore, when you create a thread object, you must explicitly define the task that you want to run.

4. Call the Thread object's Start method to open the thread.

Benefits of implementing the Runnable interface:

1. Separate the thread's tasks from the child classes of the thread, which are individually encapsulated. Encapsulates a task into an object based on object-oriented thinking.

2. Avoids the limitations of Java single inheritance.

Java Nineth Day

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.