You really will create a thread right?

Look at a problem before the start of the article, below are several ways to create what threads are, what is the difference and contact between them is?

l9Q2K1.png
If you can easily answer, then this article is too basic for you, do not Kanla down, if there is confused, then go over and Nauyus several ways to create a thread under the bar!

How to create a thread

Java thread class Object is a class whose instances are inherited from java.lang.Thread or subclass. It can be used as follows to create a thread in java:

Tread thread = new Thread();
thread.start();
复制代码

But this thread will be created to perform rapid end, because the content execution thread is empty, if you need a thread executing custom code, can be achieved in two ways, one is to inherit the Thread class, override the run method of execution threads SUMMARY run into the process, the other is to achieve Runable interface implementation classes Runable interface as a parameter passed to the Thread class. Because of the emergence of anonymous inner classes and Java8 Lambda, which in turn have their own variants of two ways, but the original aim, let's take a look.

Thread class inheritance

The first method is a Thread class inheritance, self-run method override methods defined in the parent class code execution thread transfer implemented:

public class MyThread extends Thread {
   @Override
   public void run(){
     System.out.println("MyThread running");
   }
}
MyThread myThread = new MyThread();
myThread.start();
复制代码

Thread class inheritance - anonymous inner class writing

For those who only need a code to instantiate if every time you need to define a MyThread is very troublesome, which have anonymous inner classes variants:

//匿名内部类
Thread thread = new Thread(){
   @Override
   public void run(){
     System.out.println("Thread Running");
   }
};
thread.start();

//还可以更简化
new Thread(){
   @Override
   public void run(){
     System.out.println("Thread Running");
   }
}.start();
复制代码

Achieve Runable Interface

There is a way to create multiple threads is to achieve Runable interfaces, the interface implementation class Runable passed to the Thread class as a parameter, thereby leading to delivery threads to execute the purpose of the code:

public class MyRunnable implements Runnable {
   public void run(){
    System.out.println("MyRunnable running");
   }
}
Thread thread = new Thread(new MyRunnable());
thread.start();
复制代码

Implement Runable Interface - anonymous inner class

Similarly, we can use anonymous inner classes simplify the code:

//匿名内部类
Runnable myRunnable = new Runnable(){
   public void run(){
     System.out.println("Runnable running");
   }
}
Thread thread = new Thread(myRunnable);
thread.start();

//还可以更简化
new Thread( new Runnable(){
    @Override
    public void run(){
        System.out.println("Runnable running");
    }
}).start();
复制代码

Implement the interface --Lambda expression Runable

Well, here it is the code has been simplified, but again we look at this code:

l9Q0ET.png

In fact, in addition to System.out.println("Runnable running");a body outside thread we need to perform all other template code, then why do we actually useful code should pass the entire class in order to pass a line to get in? Java8 proposed functional programming ideas, a function can be passed as a parameter to the direct method, and can function using Lambda expressions to simplify the expression, using threads Lambda expressions are created as follows:

new Thread(() -> {
    System.out.println("Thread Running");
}).start();
复制代码

to sum up

The figure below summarizes the way to create threads, of course, the practical application of our more threads to create a thread pool, but also inseparable from these foundations, more importantly, thinking, well, on the part of the thread pool our future articles Goodbye.

l9QUuq.md.png

Thanks for reading, originality is not easy, if inspired, point a praise it! This will be the most powerful engine of my writing! This article does not simultaneously released to the public numbers more than technology to technology Nauyus, to share some of the major programming languages, architecture design, cognitive thinking articles, from December 2019 to open more weeks modes, welcome attention, learn, grow!

Guess you like

Origin juejin.im/post/5e00de4ae51d4557e87fdc26