Before we start, what are the following ways of creating threads, and what are the differences and connections between them?
How do I create a thread
The Java Thread class is also an Object class, whose instances all inherit from Java.lang.Thread or its subclasses. You can create a thread in Java as follows:
Thread thread = new Thread();
thread.start();
Copy the code
However, Thread execution will end quickly because the Thread execution is empty. If you need to execute custom code, there are two ways to do this. One is to inherit the Thread class and override the run method to put the Thread execution content into the run method, or implement the Runable interface. The class implementing the Runable interface is passed as an argument to the Thread class. Thanks to anonymous inner classes and Java8 Lambda, there are variations on both approaches, but they’re all the same. Let’s take a look.
Thread class inheritance
The first is to inherit Thread and override the parent with a custom run method:
public class MyThread extends Thread {
@Override
public void run(){
System.out.println("MyThread running");
}
}
MyThread myThread = new MyThread();
myThread.start();
Copy the code
Inheriting Thread class — anonymous inner class writing
For code that only needs to be instantiated once, it can be tricky to define a MyThread each time. Here’s the anonymous inner class variant:
// Anonymous inner class Thread Thread = newThread(){
@Override
public void run(){
System.out.println("Thread Running"); }}; thread.start(); // We can simplify new even moreThread(){
@Override
public void run(){
System.out.println("Thread Running");
}
}.start();
Copy the code
Implement the Runable interface
Another way to create multiple threads is to implement the Runable interface and pass the class that implements the Runable interface as a parameter to the Thread class.
public class MyRunnable implements Runnable {
public void run(){
System.out.println("MyRunnable running");
}
}
Thread thread = new Thread(new MyRunnable());
thread.start();
Copy the code
Implement the Runable interface — anonymous inner class
Similarly, we can use anonymous inner classes to simplify the code:
// Anonymous inner class Runnable myRunnable = newRunnable(){
public void run(){
System.out.println("Runnable running"); } } Thread thread = new Thread(myRunnable); thread.start(); // We can also simplify new Thread(new)Runnable(){
@Override
public void run(){
System.out.println("Runnable running");
}
}).start();
Copy the code
Implement the Runable interface — Lambda expressions
Ok, so far the code has been very simplified, but let’s look at this code again:
Println (“Runnable running”); Is the thread body that we need to execute and everything else is template code, so why would we pass an entire class in order to pass a line of code that actually works? Java8 introduces the idea of functional programming, where a function can be passed directly to a method as an argument, and functions can be expressed using Lambda expressions to simplify the expression. Threads created using Lambda expressions are as follows:
new Thread(() -> {
System.out.println("Thread Running");
}).start();
Copy the code
conclusion
The following figure summarizes how to create a thread. Of course, in practice we use thread pools more to create a thread, but it is also important to follow these basics. More importantly, the idea of thread pools will be discussed later in this article.
This article was first published on the public account Nauyus, which is not only about technology. Please identify the qr code below to get more content. It mainly shares original technology such as JAVA, micro services, programming languages, architecture design, thinking and cognition. From December 2019, the weekly watch mode will be opened. Welcome to pay attention and learn with Nauyus.
Bonus 1: Backend development video tutorials
Over the years, dozens of sets of JAVA backend development video tutorials, including microservices, distributed,Spring Boot,Spring Cloud, design mode, cache, JVM tuning, MYSQL, large-scale distributed e-commerce project combat and other content, pay attention to Nauyus
Bonus 2: Interview questions packaged for download
Interview questions collected over the years, including job guide, interview skills, Microsoft, Huawei, Ali, Baidu and many other enterprises interview questions collected. This part is still in continuous sorting, you can continue to pay attention to. 【 Interview questions 】 No way to get.