当前位置:网站首页>Comparison between thread and runnable in creating threads

Comparison between thread and runnable in creating threads

2022-07-07 04:55:00 Hua Weiyun

One 、 By inheritance Thread Create thread

By inheritance Thread class , Create a thread , In the main thread , call start, Put the thread in runnable state , The method of letting the system run threads .

public class MyThread extends Thread {    @Override    public void run() {        System.out.println(" Execute thread ");    }}public class ThreadTest {    public static void main(String[] args) {        Thread thread = new MyThread();        thread.start();    }}

Two 、 Realization Runnable Create check

By implementing runnable Interface , Create a thread , Need to put runnable Implementation class of , As Thread The parameters of the construction method , Run this thread .

public class MyThread implements Runnable {    @Override    public void run() {        System.out.println(" Execute thread ");    }}public class ThreadTest {    public static void main(String[] args) {        Runnable thread = new MyThread();        Thread thread1 = new Thread(thread);        thread1.start();    }}

Here we should pay attention to some ,runnable Implementation class of , It's not start() Methodical , Only run() Method
 Insert picture description here
If you run this run Method , Methods that can still run threads , however , This method does not run in a new thread , It runs in the main thread .

public class MyThread implements Runnable {    @Override    public void run() {        System.out.println(" In this method :"+Thread.currentThread().getName()+" Run in ");    }}public class ThreadTest {    public static void main(String[] args) {        Runnable run = new MyThread();        System.out.println(" In this method :"+Thread.currentThread().getName()+" In the implementation of ");        run.run();// Thread thread1 = new Thread(run);// thread1.start();    }}

Running results :
 Insert picture description here
If you open the comment ,run() Comment out , give the result as follows :
 Insert picture description here

3、 ... and 、 Compare two ways to create

3.1、 Multiple inheritance

Java Multiple inheritance of classes is not supported , But it supports multiple implementations of interfaces , therefore Runnable More flexible in use .

3.2、 Data sharing

If a class inherits Thread, It is not suitable for resource sharing . But if it does Runable Interface , It's easy to share resources .
If there is a member variable in the thread , Use Runnable Threads created , Multiple threads can access this variable together , and Thread Can not be .
 Insert picture description here
Why? ? One Thread Threads created , Every time new A new thread object for , Member variables between thread objects are not shared . and Runnale, It can be used as Thread Class parameters , You can create more Thread class , hold Runnale As a parameter , Let multiple threads run together .

public class ThreadTest {    public static void main(String[] args) {        Runnable run = new MyThread();        Thread thread1 = new Thread(run);        Thread thread2 = new Thread(run);        Thread thread3 = new Thread(run);        thread1.start();        thread2.start();        thread3.start();    }}

3.3、 Thread pool

Thread pool can only be put into implementation Runable or callable Class thread , Can't put inheritance directly Thread Class .
 Insert picture description here

Four 、 Source code analysis

As we enter Thread Method , You can see Thread It has also been realized. Runnable Interface
 Insert picture description here
Use Thread It must have achieved his run Method .
 Insert picture description here
It can be seen that , In this run In the method , If target Not empty , perform target Of run Method , that target What is it? ?
 Insert picture description here
After entering , Found to be runnable Interface , How is it initialized ? All of a sudden , perform runnable Implementation class of , Will pass it through the constructor , Let's take a look at the constructor .

 Insert picture description here
Keep going , It can be seen that it is indeed initialized here .
 Insert picture description here
therefore ,Runnbale Interface run Method , It's through Thread Member variables to execute .

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207062222427630.html