Why master multithreading
The only purpose of multithreading is to make better use of CPU resources, because all multithreaded code can be implemented with a single thread. This is only half true, because the program code that reflects “multiple roles” needs at least one thread for each role, otherwise it cannot even simulate the actual scenario, and certainly cannot be implemented with a single thread: for example, the most common “producer-consumer model”. To the importance of multithreading, believe also do not need to elaborate too much, as long as did not comprehend thoroughly, the interviewer will give social education… Don’t ask me how I know!!
Create multi-threaded ways
There are various documents on the Internet about how to create multithreading, each has its own version, there are 2, 4, 6 and so on, there are return values also have thread pools and so on; How many are there? The answer to this question is already given in the official Oracle documentation. Official network file link
There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread
-
Method 1: Implement the Runnable interface
/** * Runnable */ public class RunnableStyle implements Runnable{ @Override public void run(a) { System.out.println("Implemented the Runable method."); } public static void main(String[] args) { Thread thread = new Thread(newRunnableStyle()); thread.run(); }}Copy the code
-
Method two: inherit the Thread class
public class ThreadStyle extends Thread{ @Override public void run(a) { System.out.println("Inherit the Thread class to implement multithreading."); } public static void main(String[] args) { newThreadStyle().start(); }}Copy the code
Implement quality
Method one (implementing the Runnable interface) is better
-
In terms of code architecture, the run method corresponds to a specific task and should be decoupled from the Thread mechanism (i.e., the Thread class), such as the creation and running of threads
-
Resource saving, each time a new task needs to create a separate Thread (inherited from Thread class), by implementing the Runnable method can take advantage of the Thread pool mechanism
-
Java does not support multiple inheritance, so the Thread cannot inherit from other classes, which is troublesome for business
-
From the source perspective, the implementation of Runnable has a judgment, calling the Run method passed in; By inheriting Thread, you override the entire Run method (see the source code above).
@Override public void run(a) { if(target ! =null) { target.run(); }}Copy the code
Think about what happens when you call both Runnable’s run() and Thread’s run() methods.
In the end, the Thread method executes. From a source perspective, we constructed the Thread method and passed in the Runnable object, but since the Thread implementation overwrites the run() method, we still execute the override run() method.
public class BothRunnableThread {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run(a) {
System.out.println("Run the Runnable method."); {}})@Override
public void run(a) {
System.out.println("Run the Thread method"); } }.start(); }}Copy the code
conclusion
- Creation threads fall into two categories, according to the official Oracle documentation;
- To be precise, there is only one way to create a Thread — to construct the Thread class — and there are two ways to implement the execution unit of a Thread
- Method 1: Implement the run method of the Runnable interface and pass an instance of the Runnable object to Thread for execution
- Method 2: Override the run method of Thread.