Java Multithreading Series--How the "basics" thread is created

Source: Internet
Author: User
1. Thread creation is commonly used in two ways
1)继承 Thread 类创建线程2)实现 Runnable 接口创建线程
The difference between 2.Thread and runnable
Thread 和 Runnable 的相同点:都是“多线程的实现方式”。Thread 和 Runnable 的不同点:Thread 是类,而Runnable是接口;Thread本身是实现了 Runnable 接口的类。我们知道“一个类只能有一个父类,但是却能实现多个接口”,因此Runnable具有更好的扩展性。此外,        Runnable还可以用于“资源的共享”。即,多个线程都是基于某一个Runnable对象建立的,它们会共享Runnable对象上的资源。通常,建议通过“Runnable”实现多线程!
Multithreaded example of 3.Thread and runnable multithreaded sample thread

Steps to create thread
1. Create a subclass of the thread class and override the run () method of the class, which is the task that the thread needs to complete, and the run () method is also known as the thread execution body.
2. Create an instance of the thread subclass, that is, create a threading object
3. Start the thread, that is, the start () method that invokes the subclass instance

Java Multithreading Series--How the "basics" thread is created

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.