One Java question every day [10] and one java question 10
Question
Describes the two most common methods for thread creation and their comparison.
Answer
Method 1: Inherit the Thread class implementation
Steps:
Method 2: implement the Runnable interface
Steps:
The difference between inheriting the Thread class creation Thread and implementing the Runnable interface creation Thread is that when multiple threads are created in the same class, the former actually creates multiple different Thread objects, its internal run () method is executed in the respective objects, and does not interfere with each other, just as multiple threads execute multiple tasks; the latter uses the same object to create multiple threads, so the attributes in the object are public, which is equivalent to multiple threads executing the same task.
In addition, classes that inherit the Thread class cannot inherit other classes. classes that implement the Runnable interface can also inherit other classes, which are relatively limited.
This statement may be a bit abstract. The following code will be used to explain it.
Reference Code
MyThread class
package me.huangzijian;public class MyThread extends Thread { private int num = 10; private String name; public MyThread(String name) { this.name = name; } @Override public void run() { int count = num; for (int i = 0; i < count; i++) { System.out.println(this.name + ":" + num); num--; } }}
MyRunnable class
package me.huangzijian;public class MyRunnableThread implements Runnable { private int num = 10; @Override public void run() { int count = num; for (int i = 0; i < count; i++) { System.out.println(Thread.currentThread().getName() + ":" + num); num--; } }}
TheadCreation class
Package me. huangzijian; public class ThreadCreation {public static void main (String [] args) {// inherit the Thread class implementation MyThread myThread1 = new MyThread ("MyThread1 "); myThread myThread2 = new MyThread ("MyThread2"); MyThread myThread3 = new MyThread ("MyThread3"); myThread1.start (); myThread2.start (); myThread3.start (); // implement the Runnable interface MyRunnableThread myRunnableThread = new MyRunnableThread (); Thread t1 = new Thread (myRunnableThread, "failed"); Thread t2 = new Thread (myRunnableThread, "MyRunnableThread2 "); thread t3 = new Thread (myRunnableThread, "MyRunnableThread3"); t1.start (); t2.start (); t3.start ();}}
Running result:
After running ThreadCreation, we can see that the result of inheriting the Thread class is as follows:
As you can see, the three threads perform operations on the field num from 10 to 1.
The Runnable interface is implemented as follows:
We can see that the three threads perform common operations on the num of the same myRunnableThread object. According to the program, each thread loops for 10 times, so num is reduced from 10 to negative. Some may ask why all three threads get 10 at the beginning, which is a thread synchronization problem. You need to use keywords such as synchronized for modification.