Before introducing java multithreading, let's take a look at the following questions.
What is multithreading?
Simply put, the cpu executes multiple tasks at the same time, which is multi-thread. (In essence, when it comes to the concepts of processes and threads, the above statement is not appropriate. However, it cannot be understood simply .)
What problems does multithreading solve?
Multithreading improves computer efficiency by making full use of cpu.
When multithreading is used, the efficiency will be improved?
The answer is no. Multithreading can improve efficiency when appropriate. However, if multithreading is abused, the efficiency may be reduced sometimes. Then, when multithreading is used, the efficiency will be improved. To solve this problem, you must understand the cpu scheduling principle in the operating system. Also, it is best to know how many CPUs your computer has and how many cores each cpu has. This plays a crucial role in determining how many threads are enabled.
Multithreading is not omnipotent, but it is absolutely impossible without multithreading. Therefore, multithreading is what we need to know. However, knowing when to use multithreading is far more important than knowing how to use multithreading. This is an instantiated object "Don't know, don't know.
Next let's take a look at how multithreading is used in java.
There are three methods to create a thread in java
1. inherit the Thread class directly and override its run method.
2. Implement the Runnable interface and its run Method
3. Implement the Callable interface and its call Method
In this blog, we use an example to introduce the first method-inheriting the Thread class. There is a string array. Now we need to use multiple threads to print the length of each string in the array separately. The Code is as follows:
<Span style = "font-family: Microsoft YaHei; font-size: 18px;"> public class Test extends Thread {private String word; public Test (String word) {this. word = word;} @ Overridepublic void run () {System. out. println (Thread. currentThread (). getName () + ": start execution! "); Try {// assume that it takes 2 seconds to process the Thread. currentThread (). sleep (2000);} catch (InterruptedException e) {e. printStackTrace ();} System. out. println (Thread. currentThread (). getName () + ": processing! "); System. out. println (Thread. currentThread (). getName () + ":" + word + "Length:" + word. length ();} public static void main (String [] args) {String [] words = {"first", "second", "world", "thread "}; for (String word: words) {Test test = new Test (word); test. start () ;}}</span>
The execution result is as follows:
<Span style = "font-family: Microsoft YaHei; font-size: 18px;"> Thread-1: start execution! Thread-2: start execution! Thread-3: start execution! Thread-0: start execution! Thread-2: processing! Thread-2: world length: 5Thread-1: processing! Thread-1: second length: 6Thread-3: processing! Thread-0: processing! Thread-3: thread length: 6Thread-0: first length: 5 </span>
Using this method to implement multithreading is simple and convenient, but it requires that classes inherit the Thread class. The same class in java cannot inherit multiple classes at the same time, which makes the operation inconvenient. At the same time, this method cannot achieve the purpose of data sharing. For details about how to achieve data sharing, I will go to the next blog.