Java-two ways to create a thread

Source: Internet
Author: User
1. Inheriting the thread class
  • Defines a class that inherits the thread class.
  • Override the Run method.
  • Instantiate the subclass object, call the Start () method, and call the Run method.
2. Implement the Runnable interface
  • Defines the class implementation runnable interface.
  • Overwrites the Run method in the interface, encapsulating the thread's task code into the Run method.
  • The thread object is created through the thread class and the subclass object of the Runnable interface is used as a parameter to the constructor of the thread class. Because the task of the thread object is encapsulated in the Run method of the Runnable interface subclass object, you must explicitly define the task to run when you create it.
  • Invokes the start () method of the Thread object.

The second method is more commonly used:

  • Separate the subclass tasks into separate packages.
  • Avoids the limitations of Java single inheritance.

Java-two ways to create a 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.