Thread的run()与start()的区别
Java中thread的start()和run()的区别:
1.start()方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码:
通过调用Thread类的start()方法来启动一个线程,
这时此线程是处于就绪状态,
并没有运行。
然后通过此Thread类调用方法run()来完成其运行操作的,
这里方法run()称为线程体,
它包含了要执行的这个线程的内容,
Run方法运行结束,
此线程终止,
而CPU再运行其它线程,
2.run()方法当作普通方法的方式调用,程序还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码:
而如果直接用Run方法,
这只是调用一个方法而已,
程序中依然只有主线程--这一个线程,
其程序执行路径还是只有一条,
这样就没有达到写线程的目的。
举例说明一下:
记住:线程就是为了更好地利用CPU,
提高程序运行速率的!
public class TestThread1{
public static void main(String[] args){
Runner1 r=new Runner1();
//r.run();//这是方法调用,而不是开启一个线程
Thread t=new Thread(r);//调用了Thread(Runnable target)方法。且父类对象变量指向子类对象。
t.start();
for(int i=0;i<100;i++){
System.out.println("进入Main Thread运行状态");
System.out.println(i);
}
}
}
class Runner1 implements Runnable{ //实现了这个接口,jdk就知道这个类是一个线程
public void run(){
for(int i=0;i<100;i++){
System.out.println("进入Runner1运行状态");
System.out.println(i);
}
}
}
同时摘取一段外文网站论坛上的解释:
Why do we need start() method in Thread class? In Java API description for Thread class is written : "Java Virtual Machine calls the run method of this thread..".
Couldn't we call method run() ourselves, without doing double call: first we call start() method which calls run() method? What is a meaning to do things such complicate?
There is some very small but important difference between using start() and run() methods. Look at two examples below:
Example one:
Code:
Thread one = new Thread();
Thread two = new Thread();
one.run();
two.run();
Example two:
Code:
Thread one = new Thread();
Thread two = new Thread();
one.start();
two.start();
The result of running examples will be different.
In Example one the threads will run sequentially: first, thread number one runs, when it exits the thread number two starts.
In Example two both threads start and run simultaneously.
Conclusion: the start() method call run() method asynchronously (does not wait for any result, just fire up an action), while we run run() method synchronously - we wait when it quits and only then we can run the next line of our code.
Thread的run()与start()的区别相关推荐
- 【Vue】npm run serve 和 npm run dev 有什么区别
[Vue]npm run serve 和 npm run dev 有什么区别 Q: 我的粉丝私信我,项目中运行的npm run serve 和 npm run dev 有什么区别?什么时候用npm r ...
- Jvm处理Java Thread 的run方法中抛出异常的流程
Jvm处理Java Thread 的run方法中抛出异常的流程 参考文章: (1)Jvm处理Java Thread 的run方法中抛出异常的流程 (2)https://www.cnblogs.com/ ...
- 别傻傻分不清docker run 和 start 的区别了
docker run 和 start 的区别 1.docker run docker run 只在第一次运行时使用,将镜像放到容器中,以后再次启动这个容器时,只需要使用命令docker start 即 ...
- WScript.Shell对象的这两个方法Run和Exec的区别
转载自:http://blog.sina.com.cn/s/blog_4b8d35b70100twah.html Set ws = CreateObject("WScript.Shell&q ...
- 请说明Thread类中run和start的区别
作用功能不同: run方法的作用是描述线程具体要执行的任务: start方法的作用是真正的去申请系统线程 运行结果不同: run方法是一个类中的普通方法,主动调用和调用普通方法一样,会顺序执行一次: ...
- java.lang.Thread 和 java.lang.Runnable的区别
thread是类 runnable是接口实现多线程有两种方式: 1.继承Thread,然后重写他的run方法 2.实现Runnable接口,并实现他的run方法启动线程时也有区别 继承Thread的类 ...
- Task.Factory.StartNew 和 Task.Run 到底有什么区别?
前言 Task.Factory.StartNew 和 Task.Run 都可以创建 Task: Task.Factory.StartNew(() => { Console.WriteLine(& ...
- QThread使用——关于run和movetoThread的区别
QThread 使用探讨 2010-10-23 00:30 注意:本文停止更新,请优先考虑 Qt 线程基础(QThread.QtConcurrent等) dbzhang800 2011.06.18 Q ...
- Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别
查了一些资料也不是太明白两个的区别,但是前者是最安全的用法 打个简单的比方,你一个WEB程序,发布到Tomcat里面运行. 首先是执行Tomcat org.apache.catalina.startu ...
最新文章
- Strategy模式
- 转贴 周鸿祎充其量算作一个低级商人
- DOS窗口的编码页从UTF-8调回GBK
- C语言学习及应用笔记之四:C语言volatile关键字及其使用
- IntelliJ IDEA 2020.2 正式发布,真香!
- php统一处理异常,PHP异常处理
- h5 html被缓存,【Web前端问题】html5离线缓存,不能被缓存问题
- java线程释放_Java多线程出现异常会自动释放锁
- Linux源码编译安装
- 优酷视频kux格式转mp4格式
- unity漂移 unity3d教程 // WheelCollider
- Java设计原则——依赖倒转原则
- Python经典例题:跑马灯文字效应
- 之前的账号不见了,@21cn.com的。哪里可以找的回来?
- StringUtils中isNotEmpty和isNotBlank及isBlank()和isEmpty()区别
- Smartbi自定义生成报表-制作流程
- SnapMotion for Mac(Mac从视频中截图软件)
- 中通新财报:业绩下滑背后仍是老问题
- 无线网卡性能怎么测试软件,Mercury MW150U 无线网卡性能测试
- 网上关于画板子用什么软件的讨论
热门文章
- go环境搭建_学习的golang第一步,搭建我们运行的环境,go! go! go
- python socket编程_Python学习记录-socket编程
- iphone已停用怎么解锁_两种无密码解锁iPhone锁屏密码的方法
- 基于html人事管理报告,基于C++builder的课程设计报告 (人事管理系统)
- ModuleNotFoundError: No module named ‘apt_pkg‘
- 【渝粤教育】电大中专幼儿园课程论 (10)作业 题库
- [渝粤教育] 中国地质大学 高层建筑施工 复习题
- 渝粤教育 陕西师范大学 《金融中介学Ⅰ》作业
- springboot security 权限校验_springboot借助aop和注解实现权限校验
- 计算机电路的基本罗门,模拟电路设计经验12条