当前位置:网站首页>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 .
边栏推荐
- A series of shortcut keys for jetbrain pychar
- ACL2022 | 分解的元学习小样本命名实体识别
- 过气光刻机也不能卖给中国!美国无理施压荷兰ASML,国产芯片再遭打压
- MySQL forgot how to change the password
- Common Oracle SQL statements
- npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
- A picture to understand! Why did the school teach you coding but still not
- Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million
- Zhou Yajin, a top safety scholar of Zhejiang University, is a curiosity driven activist
- 《原动力 x 云原生正发声 降本增效大讲堂》第三讲——Kubernetes 集群利用率提升实践
猜你喜欢
【实践出真理】import和require的引入方式真的和网上说的一样吗
Vscode automatically adds a semicolon and jumps to the next line
What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash
5G VoNR+之IMS Data Channel概念
Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
【736. Lisp 语法解析】
A simple and beautiful regression table is produced in one line of code~
Tree map: tree view - draw covid-19 array diagram
Introduction to the PureMVC series
A line of R code draws the population pyramid
随机推荐
【數模】Matlab allcycles()函數的源代碼(2021a之前版本沒有)
【736. Lisp 语法解析】
Oracle -- 视图与序列
抖音或将推出独立种草社区平台:会不会成为第二个小红书
What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures
DFS和BFS概念及实践+acwing 842 排列数字(dfs) +acwing 844. 走迷宫(bfs)
两个div在同一行,两个div不换行「建议收藏」
What work items do programmers hate most in their daily work?
DFS and BFS concepts and practices +acwing 842 arranged numbers (DFS) +acwing 844 Maze walking (BFS)
A simple and beautiful regression table is produced in one line of code~
Organize five stages of actual attack and defense drill
offer如何选择该考虑哪些因素
acwing 843. N-queen problem
ACL2022 | 分解的元学习小样本命名实体识别
A series of shortcut keys for jetbrain pychar
Lecture 3 of "prime mover x cloud native positive sounding, cost reduction and efficiency enhancement lecture" - kubernetes cluster utilization improvement practice
Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
Meow, come, come: do you really know if, if else
What is JVM? What are the purposes of JVM tuning?