Thread 的run方法和start方法的区别
2024-10-21 05:55:12
start()方法是用来启动线程,实现了多线程运行
点进去查看源码,发现start方法创建一个线程 并让线程处于就绪状态,并且在start方法内会调用start0()方法,而start0作为本地方法 会用c/c++创建一个线程并且调用run方法运行
private native void start0();
@Override
public void run() {
if (target != null) {
target.run();
}
}
private void exit() {
if (group != null) {
group.threadTerminated(this);
group = null;
}
threadLocals = null;
inheritableThreadLocals = null;
inheritedAccessControlContext = null;
blocker = null;
uncaughtExceptionHandler = null;
}
public synchronized void start() {
if (threadStatus != 0)
throw new IllegalThreadStateException();
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 */
}
}
}
而run 方法只是当成普通方法调用,程序还是主线程运行。
注:
Thread 的run方法是运行上一个线程
//而 THread 的start 的方法是创建一个线程并且运行
//尽量实现 runnable 接口 因为有线程池
最新文章
- ios 向工程里添加Fonts
- vtigerCRM5.4的安装和汉化 ubuntu
- swift 开眼今日精选
- zend studio一些常用配置
- 293.	Flip Game
- 博弈论(SG函数):HNOI 2007 分裂游戏
- android之View的启动过程
- 【Sort】RadixSort基数排序
- CentOS7 开源跳板机(堡垒机) Jumpserver
- 第二章 js数据类型和变量
- Xmanager6 下载地址
- @EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter区别
- Redis的使用及参考代码
- numpy中random的使用
- 【CentOS】JDK的安装
- noj装载问题
- 和嗲妹妹面试python,是种什么体验?
- sahrepoint 上传到文档库
- Codeforces 799D Field expansion - 搜索 - 贪心
- Linux crontab使用方法