当前位置:网站首页>Thread和Runnable创建线程的方式对比
Thread和Runnable创建线程的方式对比
2022-07-06 22:23:00 【华为云】
一、通过继承Thread创建线程
通过继承Thread类,创建一个线程,在主线程中,调用start,让线程处于runnable状态,让系统去运行线程的方法。
public class MyThread extends Thread { @Override public void run() { System.out.println("执行线程"); }}public class ThreadTest { public static void main(String[] args) { Thread thread = new MyThread(); thread.start(); }}
二、实现Runnable创建检查
通过实现runnable接口,创建一个线程,需要把runnable的实现类,作为Thread的构造方法的参数,运行这个线程。
public class MyThread implements Runnable { @Override public void run() { System.out.println("执行线程"); }}public class ThreadTest { public static void main(String[] args) { Runnable thread = new MyThread(); Thread thread1 = new Thread(thread); thread1.start(); }}
这里要注意一些,runnable的实现类,是没有start()方法的,只有run()方法
如果运行这个run方法,依然能运行线程的方法,但是,这个方法并不是在新的线程中运行的,而是在主线程中运行的。
public class MyThread implements Runnable { @Override public void run() { System.out.println("此方法在:"+Thread.currentThread().getName()+"中运行"); }}public class ThreadTest { public static void main(String[] args) { Runnable run = new MyThread(); System.out.println("此方法在:"+Thread.currentThread().getName()+"中执行"); run.run();// Thread thread1 = new Thread(run);// thread1.start(); }}
运行结果:
如果把注释打开,run()注释掉,结果如下:
三、比较两种创建方式
3.1、多继承
Java是不支持类的多继承的,但是支持接口的多实现,所以Runnable在使用上更加灵活。
3.2、 数据共享
如果一个类继承Thread,则不适合资源共享。但是如果实现了Runable接口的话,则很容易的实现资源共享。
如果线程中有一个成员变量,使用Runnable创建的线程,多个线程之间可以共同访问这个变量,而Thread不可以。
为什么呢?一个Thread创建的线程,每次都是new的一个新的线程对象,线程对象之间的成员变量不共享。而Runnale,可以作为Thread类的参数,可以多创建几个Thread类,把Runnale作为参数,让多个线程一起运行。
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、线程池
线程池只能放入实现Runable或callable类线程,不能直接放入继承Thread的类。
四、源码分析
当我们进入Thread方法,可以看的Thread也实现了Runnable接口
使用Thread肯定也实现了他的run方法。
可以看出,在这个run方法中,如果target不为空,执行target的run方法,那target是什么?
进入后,发现是runnable接口,那它是如何初始化的?突然想起,执行runnable的实现类时,都会把他通过构造函数传递,那来看看构造函数。
继续往下,可以看出确实是在这里初始化的。
所以,Runnbale接口的run方法,是通过Thread的成员变量去执行的。
边栏推荐
- 每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报
- 【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
- SQL where multiple field filtering
- Advertising attribution: how to measure the value of buying volume?
- Jetson nano配置pytorch深度学习环境//待完善
- 【数模】Matlab allcycles()函数的源代码(2021a之前版本没有)
- 计数排序基础思路
- 5G VoNR+之IMS Data Channel概念
- Zhou Yajin, a top safety scholar of Zhejiang University, is a curiosity driven activist
- Run the command once per second in Bash- Run command every second in Bash?
猜你喜欢
【实践出真理】import和require的引入方式真的和网上说的一样吗
JS also exports Excel
NTU notes 6422quiz review (1-3 sections)
DFS and BFS concepts and practices +acwing 842 arranged numbers (DFS) +acwing 844 Maze walking (BFS)
Common Oracle SQL statements
深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚
On the 110th anniversary of Turing's birth, has the prediction of intelligent machine come true?
mpf2_线性规划_CAPM_sharpe_Arbitrage Pricin_Inversion Gauss Jordan_Statsmodel_Pulp_pLU_Cholesky_QR_Jacobi
Network Security Learning - Information Collection
The easycvr platform is connected to the RTMP protocol, and the interface call prompts how to solve the error of obtaining video recording?
随机推荐
窗口可不是什么便宜的东西
How to open win11 remote desktop connection? Five methods of win11 Remote Desktop Connection
每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报
Thesis landing strategy | how to get started quickly in academic thesis writing
Windows are not cheap things
Basic idea of counting and sorting
3GPP信道模型路损基础知识
Deeply cultivate the developer ecosystem, accelerate the innovation and development of AI industry, and Intel brings many partners together
微信能开小号了,拼多多“砍一刀”被判侵权,字节VR设备出货量全球第二,今日更多大新闻在此
The easycvr platform is connected to the RTMP protocol, and the interface call prompts how to solve the error of obtaining video recording?
What work items do programmers hate most in their daily work?
Detect when a tab bar item is pressed
浙江大学周亚金:“又破又立”的顶尖安全学者,好奇心驱动的行动派
R语言主成分pca、因子分析、聚类对地区经济研究分析重庆市经济指标
Fix the problem that the highlight effect of the main menu disappears when the easycvr Video Square is clicked and played
SQL where multiple field filtering
Zhou Yajin, a top safety scholar of Zhejiang University, is a curiosity driven activist
What is JVM? What are the purposes of JVM tuning?
Organize five stages of actual attack and defense drill
Both primary and secondary equipment numbers are 0