当前位置:网站首页>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 .
边栏推荐
- Thesis landing strategy | how to get started quickly in academic thesis writing
- Canteen user dish relationship system (C language course design)
- jvm是什么?jvm调优有哪些目的?
- Break the memory wall with CPU scheme? Learn from PayPal to expand the capacity of aoteng, and the volume of missed fraud transactions can be reduced to 1/30
- offer如何选择该考虑哪些因素
- 【实践出真理】import和require的引入方式真的和网上说的一样吗
- 架构实战训练营|课后作业|模块 6
- 广告归因:买量如何做价值衡量?
- Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
- Detect when a tab bar item is pressed
猜你喜欢
Intel and Xinbu technology jointly build a machine vision development kit to jointly promote the transformation of industrial intelligence
Vscode 如何使用内置浏览器?
Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
Introduction to the PureMVC series
In depth analysis of kubebuilder
A row of code r shows the table of Cox regression model
窗口可不是什么便宜的东西
R语言主成分pca、因子分析、聚类对地区经济研究分析重庆市经济指标
5G VoNR+之IMS Data Channel概念
Meow, come, come: do you really know if, if else
随机推荐
jvm是什么?jvm调优有哪些目的?
ServiceMesh主要解决的三大痛点
Oracle - views and sequences
JS variable case output user name
DFS和BFS概念及实践+acwing 842 排列数字(dfs) +acwing 844. 走迷宫(bfs)
Zhou Yajin, a top safety scholar of Zhejiang University, is a curiosity driven activist
【数模】Matlab allcycles()函数的源代码(2021a之前版本没有)
Run the command once per second in Bash- Run command every second in Bash?
AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务
On the 110th anniversary of Turing's birth, has the prediction of intelligent machine come true?
【數模】Matlab allcycles()函數的源代碼(2021a之前版本沒有)
What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures
Common Oracle SQL statements
使用Thread类和Runnable接口实现多线程的区别
Leetcode notes
A row of code r shows the table of Cox regression model
每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报
STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
Thread和Runnable创建线程的方式对比
Is there any way to bookmark the code in the visual studio project- Is there a way to bookmark code in a Visual Studio project?