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