Multithreading in Java allows our program to run multiple tasks at the same time, even if our CPU is single-core. Of course, we all understand that in this case, running at the same time is not really running at the same time, but the thread scheduler in the JVM can quickly switch between different threads based on the time slice rotation method. The thread scheduler allows the JVM to run this thread for a while, and then run that thread for a while. The switching speed quickly produces the illusion that these threads seem to run simultaneously.
There are two ways to create a thread. What are the differences between the two methods? What are the differences in performance? Why do we need to design two methods? With these questions, let's first review the two methods of thread creation.
The first method is to create a thread by implementing the Runnable interface:
Public class StudyThread {public static void main (String [] args) {MyRunnable myRunnable = new MyRunnable (); Thread thread = new Thread (myRunnable); thread. start () ;}} class MyRunnable implements Runnable {public void run () {System. out. println ("method 1 ");}}
The second method creates a Thread by inheriting the Thread class:
Public class StudyThreadTwo {public static void main (String [] args) {MyThread myThread = new MyThread (); myThread. start () ;}} class MyThread extends Thread {public void run () {System. out. println ("second Startup Mode ");}}
Both methods can create a new thread normally. I think there should be little difference in performance. Let's look at the differences between them from the design perspective.
The first method is the most frequently used method. It creates a Thread and requires two related classes, one is the Thread class provided by Java, and the other is implemented by ourselves: implement the Runnable interface and implement the run method. What we need to do is to create a Thread object. We need to implement a Runnable parameter during creation. In practice, we usually use anonymous internal classes to implement this method, as shown in the following code:
Public class StudyThread {public static void main (String [] args) {Thread thread = new Thread (new Runnable () {public void run () {System. out. println ("using anonymous internal classes to create Threads") ;}}); thread. start ();}}
The second method requires only one class, that is, we inherit from the Thread class, and then override the run method. This method is simpler than the first one. In fact, this simplicity is costly. If we consider it from the design perspective (design principles, design patterns), we will understand why we rarely use this approach in practice.
According to the single Responsibility Principle in the design principle, our class should only implement one type of function, and there should be only one reason for class change. The class displayed on the interface should be responsible for the interface display only, data processing is only responsible for data processing, and only for control. The most suitable example is the MVC composite design mode. Let's go back to the above analysis. To make it easy to understand, we can regard the Thread class as a worker. Runnable (the run method in) is the work of this worker ), in the first method of creation, the worker is a worker, the work is a loose coupling, and the change of the Runnable does not affect the worker (Thread). In the second method of creation, the worker and the work are bound to death (tightly coupled), and the work changes, and the workers also change. In a simple project, it is better to say that, if you want to change the entire system in one of your large projects, you and your project will crash together.
Since the second one has such drawbacks as the first one, why do we need to design the first one? I think we can understand it from the inheritance perspective. Inheritance allows the subclass to automatically share the data and methods of the parent class, and allows the subclass to add new data or methods. Sometimes we need to bind a job to a worker, and if it is used in large quantities, the second method has advantages. I do not know whether this understanding method is suitable or not. You are welcome to participate actively in the discussion.