线程中start和run方法的区别
先说java中实现多线程常用的两种方式:
1:继承Thread类,并重写run()方法
2:实现Runnable接口,实现run方法
实际上Thread类也是实现了Runnable接口
1
2
3
|
public class Thread implements Runnable { ........ } |
而Runnable接口只定义了一个run方法
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
@FunctionalInterface public interface Runnable { / * * * When an object implementing interface < code > Runnable < / code > is used * to create a thread , starting the thread causes the object 's * < code > run < / code > method to be called in that separately executing * thread. * < p > * The general contract of the method < code > run < / code > is that it may * take any action whatsoever. * * @see java.lang.Thread #run() * / public abstract void run ( ) ; } |
我们需要实现的多线程业务逻辑,都需要在run方法内实现。
而start()是Thread中定义的方法,以下是start()方法的源码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
public synchronized void start() { /** * This method is not invoked for the main method thread or "system" * group threads created/set up by the VM. Any new functionality added * to this method in the future may have to also be added to the VM. * * A zero status value corresponds to state "NEW". */ if (threadStatus != 0 ) throw new IllegalThreadStateException(); /* Notify the group that this thread is about to be started * so that it can be added to the group's list of threads * and the group's unstarted count can be decremented. */ group.add(this); boolean started = false; try { start0(); started = true; } finally { try { if (!started) { group.threadStartFailed(this); } } catch (Throwable ignore) { /* do nothing. If start0 threw a Throwable then it will be passed up the call stack */ } } } |
start()通过调用本地native方法start0来启动一个线程,本地方法内最终会调用run方法,这样就可以实现在另一个线程中运行我们需要的代码。
而单独运行run()方法,就跟我们调用普通接口实现方法一样了,并不会启动新的线程.
总结:
1、start()方法运行时,本地启动新的线程,线程中会调用run方法,程序不需要等待run方法执行完毕再执行后面的程序;
2、run()方法是我们编写需要新的线程执行代码的方法,单独运行是不会有多线程效果的。
更多技术资讯可关注:itheimaGZ获取
线程中start和run方法的区别的更多相关文章
- Java -- Thread中start和run方法的区别
一.认识Thread的 start() 和 run() 1.start(): 我们先来看看API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并发 ...
- Java中start和run方法的区别
一.问题引入 说到这两个方法就不得不说多线程,说到多线程就不得不提实现多线程的两种方式继承Thread类和实现Runable接口,下面先看这两种方式的区别. 二. Java中实现多线程 ...
- 认识多线程中start和run方法的区别?
一.认识多线程中的 start() 和 run() 1.start(): 先来看看Java API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并 ...
- 线程的start和run方法的区别
回到这个问题,可以用源码的角度去回答,这样会让面试官对有更好的印象 ------>如果直接调用run方法的话,所执行的线程是main线程.调用start方法的话,会新建一个子线程,去执行run方 ...
- Java线程中yield与join方法的区别
长期以来,多线程问题颇为受到面试官的青睐.虽然我个人认为我们当中很少有人能真正获得机会开发复杂的多线程应用(在过去的七年中,我得到了一个机会),但是理解多线程对增加你的信心很有用.之前,我讨论了一个w ...
- 【Java_多线程并发编程】基础篇—Thread类中start()和run()方法的区别
1. start() 和 run()的区别说明 start()方法: 它会启动一个新线程,并将其添加到线程池中,待其获得CPU资源时会执行run()方法,start()不能被重复调用. run()方法 ...
- 线程中start与run方法的主要区别
区别一: 在于当程序调用start方法一个新线程将会被创建,并且在run方法中的代码将会在新线程上运行, 然而在你直接调用run方法的时候, ...
- 线程中sleep和wait方法的区别
sleep() 方法: 线程主动放弃CPU,使得线程在指定的时间内进入阻塞状态,不能得到CPU 时间,指定的时间一过,线程重新进入可执行状态.典型地,sleep()被用在等待某个资源就绪的情形:测试发 ...
- 线程的状态有哪些,线程中的start与run方法的区别
线程在一定条件下,状态会发生变化.线程一共有以下几种状态: 1.新建状态(New):新创建了一个线程对象. 2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法. ...
随机推荐
- MySQL视图和事务
视图的操作 ...
- Linux下idea由于缺少相关权限导致的tomcat ERROR
昨天一天都在倒腾两个系统,也是醉了. 不过还好,系统修好了,在ubuntu下重新安装idea后,出现了这个错误: Intellij Idea Tmocat Error running Tomcat: ...
- inotifywait命令详解及安装
https://www.cnblogs.com/pyrene/p/6414724.html 安装 https://www.cnblogs.com/martinzhang/p/4126907.h ...
- python中selenium自动化常用关键字
一:定位八种方法 例如: 二:常见的webdriver方法 1.浏览器相关:(打开浏览器先导入webdriver模块) (1)set_window_size(480,800)调整浏览器宽高大小 (2) ...
- Aras Innovator获取项目任务序列号
//方法名:GetProjectTasksNumber //功能描述:获取项目任务序列号 //原作者:joe //创建时间:20141225 //版权所有(C)JOE.FAN //---------- ...
- Fib数列问题(项数很大)
用fib(n)表示斐波那契数列的第n项,现在要求你求fib(n) mod m.fib(1)= 1, fib(2)= 1. 输入格式 输入2个整数n(1≤n≤1018), m(2≤m≤10000000) ...
- 01 语言基础+高级:1-6 集合_day04【Map】
day04 [Map] 主要内容 Map集合 教学目标 能够说出Map集合特点 使用Map集合添加方法保存数据 使用”键找值”的方式遍历Map集合 使用”键值对”的方式遍历Map集合 能够使用Hash ...
- 宝塔面板,Typecho,Lsky Pro图床安装
宝塔面板,Typecho,Lsky Pro图床安装 本文安装包:链接:https://cloud.cangye.me/s/l3i4avjl 密码是宝塔面板4个汉字拼音首字母小写(防止被爬) 一,宝塔面 ...
- 题解 P6004 【[USACO20JAN]Wormhole Sort S】
这题真的是非常标准的模板题啊 看到连最少的边,第一时间会想到 \(kruskal\) .这道题的难点其实就一个:你要注意到连边权最大的边使整个图联通 为什么:题意是第i个点想走到 \(pos[i]\) ...
- Jupyter notebook 和 Jupyter lab 的区别
Jupyter Notebook Jupyter Notebook 是一个款以网页为基础的交互计算环境,可以创建Jupyter的文档,支持多种语言,包括Python, Julia, R等等.广泛用于数 ...