当前位置:网站首页>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
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 :
If you open the comment ,run() Comment out , give the result as follows :
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 .
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 .
Four 、 Source code analysis
As we enter Thread Method , You can see Thread It has also been realized. Runnable Interface
Use Thread It must have achieved his run Method .
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? ?
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 .
Keep going , It can be seen that it is indeed initialized here .
therefore ,Runnbale Interface run Method , It's through Thread Member variables to execute .
边栏推荐
- Poor math students who once dropped out of school won the fields award this year
- 赠票速抢|行业大咖纵论软件的质量与效能 QECon大会来啦
- Intel and Xinbu technology jointly build a machine vision development kit to jointly promote the transformation of industrial intelligence
- 论文上岸攻略 | 如何快速入门学术论文写作
- File upload vulnerability summary
- 軟件測試之網站測試如何進行?測試小攻略走起!
- R language principal component PCA, factor analysis, clustering analysis of regional economy analysis of Chongqing Economic Indicators
- sscanf,sscanf_s及其相关使用方法「建议收藏」
- 过气光刻机也不能卖给中国!美国无理施压荷兰ASML,国产芯片再遭打压
- You can't sell the used lithography machine to China! The United States unreasonably pressured the Dutch ASML, and domestic chips were suppressed again
猜你喜欢
Programmers go to work fishing, so play high-end!
What if the win11 screenshot key cannot be used? Solution to the failure of win11 screenshot key
3GPP信道模型路损基础知识
mpf2_ Linear programming_ CAPM_ sharpe_ Arbitrage Pricin_ Inversion Gauss Jordan_ Statsmodel_ Pulp_ pLU_ Cholesky_ QR_ Jacobi
Win11 control panel shortcut key win11 multiple methods to open the control panel
【736. Lisp 语法解析】
Common methods of list and map
Time complexity & space complexity
01机器学习相关规定
Introduction to namespace Basics
随机推荐
STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
谈谈讲清楚这件事的重要性
Have you got the same "artifact" of cross architecture development praised by various industry leaders?
Read of shell internal value command
A series of shortcut keys for jetbrain pychar
The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
计数排序基础思路
What if the win11 screenshot key cannot be used? Solution to the failure of win11 screenshot key
Common Oracle SQL statements
史上最全学习率调整策略lr_scheduler
深入解析Kubebuilder
Win11 control panel shortcut key win11 multiple methods to open the control panel
JS variable case
mpf2_线性规划_CAPM_sharpe_Arbitrage Pricin_Inversion Gauss Jordan_Statsmodel_Pulp_pLU_Cholesky_QR_Jacobi
Factor analysis r practice (with R installation tutorial and code)
Complimentary tickets quick grab | industry bigwigs talk about the quality and efficiency of software qecon conference is coming
Meaning of 'n:m' and '1:n' in database design
关于01背包个人的一些理解
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III
Basic idea of counting and sorting