run和start区别
一、run和start区别
类写法
import java.time.LocalDateTime;
import java.util.Random;public class Student implements Runnable{static String name="A";static int id=1;int updateId(){id++;System.out.println("Id更新。运行时间为:"+LocalDateTime.now()+",获取到的线程id为:"+Thread.currentThread().getId()+", run中id: "+id);return id;}private void updateName(){this.name+="1";System.out.println("name更新。运行时间为:"+LocalDateTime.now()+",获取到的线程id为:"+Thread.currentThread().getId()+", run中name: "+name);}@Overridepublic void run() {Random random= new Random(100);Long times= Long.valueOf( (int)(Math.random()*10))*100;System.out.println(times);try {Thread.sleep(times);updateId();} catch (InterruptedException e) {e.printStackTrace();}}
}
start
main写法
import java.lang.reflect.Executable;
import java.time.LocalDateTime;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;public class Test {public static void main(String[] args) throws InterruptedException {int begin=0;int length=10;while (begin++<length){Student student=new Student();Thread t=new Thread(student);t.start();}}
}
结果为
run
import java.lang.reflect.Executable;
import java.time.LocalDateTime;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;public class Test {public static void main(String[] args) throws InterruptedException {int begin=0;int length=10;while (begin++<length){Runnable student=new Student();student.run();}}
}
结果
由以上可见,start会创建多个线程,而run会重复一个线程。
那我们再测试了一下,调取了主函数线程,发现run线程号与主函数线程号相同。
由此我们便可以得出最后的结论:start会开辟新的线程,而run则会在当前main线程中运行,不会开辟新的函数
在主函数中调取主函数编号
run和start区别相关推荐
- start() 跟 run() 方法的区别和联系
这里还有个知识点是 start() 跟 run() 方法的区别和联系. 直接调用 start() 方法,此时线程处于一个就绪(可运行)的状态,但是并没有真正的运行.而是得到CPU 的时间片后,开始执行 ...
- run在java_Java语言start和run方法的区别
Java语言start和run方法的区别 导语:Thread类定义了一个功能,用于存储线程要运行的代码,该存储功能就是run方法.那么它和start方法有什么不同呢?让我们了解一下吧! 调用start ...
- java Thread的start和run方法的区别
一.认识Thread的 start() 和 run() 概述: t.start()会导致run()方法被调用,run()方法中的内容称为线程体,它就是这个线程需要执行的工作. 用start()来启动线 ...
- java多线程 run start_java多线程中run和start区别
run只是Thread里面的一个普通方法,start是启动线程的方法. start()方法让一个线程进入就绪队列等待分配 cpu, 分到 cpu 后才调用实现的run()方法. start()方法不能 ...
- java entrypoint_docker RUN CMD ENTRYPOINT 区别
RUN 执行命令并创建新的镜像层,RUN 经常用于安装软件包. CMD 设置容器启动后默认执行的命令及其参数,但 CMD 能够被 docker run 后面跟的命令行参数替换. ENTRYPOINT ...
- Thread start()方法和run()方法的区别
Thread start() 方法 ,是一个线程开始的方法 Thread run() 方法 ,就是一个普通的方法,可以理解为main方法里面的一个普通的方法 下面写一个demo 理解下 Thread ...
- 前端开发:npm run serve和npm run dev的区别
前端开发过程中运行Vue项目的时候,有时候使用npm run serve命令可以启动项目,有时候却会报错:有时候使用npm run dev命令可以启动项目,有时候却也会报错.是什么原因造成这种情况呢, ...
- npm install和npm run dev以及npm run build的区别
我们在开发vue.js的mvvp项目时候,会执行npm install ,npm build,npm dev等等.其实这些都和package.json有关. {"name": &q ...
- synchronized与Lock的区别与使用
原文链接 https://blog.csdn.net/u012403290/article/details/64910926 ###引言: 昨天在学习别人分享的面试经验时,看到Lock的使用.想起自己 ...
最新文章
- 重构-改善既有代码的设计:简化条件表达式(七)
- 应用DIV+CSS编码时容易犯的一些错误
- showModalDialog sesission丢失
- 使用腾讯云CDN做HTTPS的WordPress博客时要改的代码
- 关于FileSystemWatcher监听文件创建
- 为什么买入不了创业版_趋势波段买卖创业板ETF指数基金能轻松保险盈利
- error: failed to launch '/private/var/mobile/Applications/** busy解决办法
- 蓝桥杯备战(一)3n+1问题
- 移动通信原理中信道的分类
- My IDEA Keymap
- 惠普m202dw_HP LaserJet Pro M202dw 激光打印机
- three.js 凹凸贴图
- 计算机主机安装图,最新版本:计算机主机插件安装图_布法罗计算机主板安装图...
- FANUC机器人外部电缆连接示意图(一)
- Android VLc编译
- Vueb报错[WDS] Errors while compiling. Reload prevented
- bpmn业务流图注意事项,常见报错问题
- 宏基因组笔记(第二章)
- 数据结构(十六)——左高树(含合并过程详细图解)
- 【整理】扩频及CDMA知识点