线程中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
|
@FunctionalInterfacepublic 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()方法. ...
随机推荐
- UVA 10891 SUM游戏 DP
刚看到这个题目不知道怎么个DP法,有点难想到 解法如下 设置dp[i][j]代表i到j这段子序列能获得的最大值,这样,枚举m=min(m,dp[i+1到j][j],dp[i][i到j-1]),m就代表 ...
- centos7.4 测试CPU压力--命令搞定
直接输入命令CPU消耗增加: cat /dev/urandom | gzip - > /dev/null 停止: 直接Ctrl+c结束
- [Python Cookbook]Pandas: How to increase columns for DataFrame?Join/Concat
1. Combine Two Series series1=pd.Series([1,2,3],name='s1') series2=pd.Series([4,5,6],name='s2') df = ...
- ssh 账号密码登录设置
找到/etc/ssh/sshd_config文件中的 PasswordAuthentication no 改为PasswordAuthentication yes 并保存. 重启ssh服务:sudo ...
- 用CNN及MLP等方法识别minist数据集
用CNN及MLP等方法识别minist数据集 2017年02月13日 21:13:09 hnsywangxin 阅读数:1124更多 个人分类: 深度学习.keras.tensorflow.cnn ...
- socket实践编程1
1.服务器端程序编写 (1).socket (2).bind (3).listen (4).accept,返回值是一个fd,accept正确返回就表示我们已经和前来连接我的客户端之间建立了一个TCP连 ...
- Python 安装modules问题及import问题
>>>modules问题 在学习Python的数据可视化时,安装了matplotlib,在安装完成后还特意在终端测试了一下,结果显示能正常import 但是在sublime Text ...
- share团队冲刺8
团队冲刺第八天 昨天:完善代码,解决其中的问题 今天:将除登陆界面之外的界面进行修改和完善,使其美观 问题:bindview不会用,使用时出现问题
- 基本 Python 词汇
本文档介绍了要理解“使用 Python 进行地理处理”的帮助文档需要掌握的一些词汇. ! 术语 说明 Python Python 是由 Guido van Rossum 在上世纪八十年代末构想并 ...
- ubuntu18安装pytorch1.3
环境: ubuntu18 anaconda 创建一个新的环境 conda create -n env_name python=version 激活并进入环境中 conda activate env_n ...