当前位置: 首页 > news >正文

50万县城做地方网站中国培训网的证书含金量

50万县城做地方网站,中国培训网的证书含金量,菜鸟网站做图,营口东站营销网站建设线程创建之源 OK!咱们闲话少叙,直接进入正题,回顾一下通过实现Runnable接口,重写run方法创建线程的方式,真的可以创建一个线程吗?来看下面这段demo。 【代码示例1】 public class Test implements Runnab…

线程创建之源

OK!咱们闲话少叙,直接进入正题,回顾一下通过实现Runnable接口,重写run方法创建线程的方式,真的可以创建一个线程吗?来看下面这段demo。

【代码示例1】

public class Test  implements Runnable{public static void main(String[] args) {Test test = new Test();test.run();}@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+":"+"runnable线程");}
}

输出:

main:runnable线程

虽然这里我们实现了Runnable接口并重写了run方法,但执行结果中输出的线程却是主线程,这可我们调用普通的方法一样,仍旧依靠的主线程驱动,那怎么样创建一个线程呢?

【代码示例2】

public class Test  implements Runnable{public static void main(String[] args) {Test test = new Test();new Thread(test).start();}@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+":"+"runnable线程");}
}

输出:

Thread-0:runnable线程

这个demo中,我们在外面套了一层Thread,然后调用start方法,最终输出的结果就是一个全新的Thread-0线程,从而实现了线程的创建。

得出结论

我们继续换Callable、FutureTask、ThreadGroup、匿名内部类或Lambda表达式等类或接口,发现均无法直接创建一个线程,必须借助Thread的start();

而例如ExecutorService线程池、ForkJoin线程池、CompletableFuture类、Timer定时器类、parallelStream并行流等等,如果有去看过它们源码的小伙伴应该清楚,它们最终都依赖于Thread.start()方法创建线程。

因此,我们在这里可以大胆的得出这样的一个结论:

在Java中创建线程的方式只有一种:通过Thread.start()调用 start()方法,会启动一个线程并使线程进入就绪状态,当分配到时间片后开始运行。 start() 会执行线程的相应准备工作,然后自动执行 run() 方法的内容

线程体与线程的区别

文章写到这里,我们一起再来思考一个问题,既然Runnable和Callable接口和Thread类一样需要重写他们提供的run()/call()方法,又没有创建线程,那它们究竟做了什么呢?这个直接给出答案:他们经过重写,确定了线程体,那线程体与线程又有何区别?我们来看看文心一言怎么说。

image

总结一句话:线程体是线程的核心部分,负责执行线程的具体任务。

所以说无论是Thread中的run还是Runnable中的run,Callable中的call方法,内部所实现的都是线程需要执行的具体内容也就是线程体

总结

基于以上的分析,若我们在面试中再次遇到:“Java线程有几种创建方式?”的考题,就可以这样回答啦:

Java中创建线程的方式有很多种,在《Java技术卷》和《Java编程思想》中提供了实现Runnable、Callable接口、继承Thread类、创建线程池这四种常见方式,我们还可以通过ForkJoin线程池、CompletableFuture类、Timer定时器类、parallelStream并行流、匿名内部类或Lambda表达式等多种方式去实现,但这些都不是真正意义上的创建线程,严格意义上,Java创建线程的方式只有一种那就是通过new Thread().start()创建,Runnable、Callable接口只是重写了线程的线程体,用来确定我们线程需要执行的内容。

文章转载自:JavaBuild

原文链接:https://www.cnblogs.com/JavaBuild/p/18060227

体验地址:引迈 - JNPF快速开发平台_低代码开发平台_零代码开发平台_流程设计器_表单引擎_工作流引擎_软件架构


文章转载自:
http://dinncosancta.zfyr.cn
http://dinncopawnbroking.zfyr.cn
http://dinncosuk.zfyr.cn
http://dinncosurveyal.zfyr.cn
http://dinncostoried.zfyr.cn
http://dinncoecogeographic.zfyr.cn
http://dinncosciolist.zfyr.cn
http://dinncodiggings.zfyr.cn
http://dinncorhinosalpingitis.zfyr.cn
http://dinncotranstainer.zfyr.cn
http://dinncoheld.zfyr.cn
http://dinncoparaleipsis.zfyr.cn
http://dinncoparadoxical.zfyr.cn
http://dinncoairy.zfyr.cn
http://dinncoswingby.zfyr.cn
http://dinncolawyer.zfyr.cn
http://dinncoinappetence.zfyr.cn
http://dinncomilking.zfyr.cn
http://dinncofalconry.zfyr.cn
http://dinncorematch.zfyr.cn
http://dinncodemilance.zfyr.cn
http://dinncoantasthmatic.zfyr.cn
http://dinncoprocurator.zfyr.cn
http://dinncoclimatotherapy.zfyr.cn
http://dinncoepileptoid.zfyr.cn
http://dinncocertify.zfyr.cn
http://dinncounthinking.zfyr.cn
http://dinncoexhaustion.zfyr.cn
http://dinncoextensive.zfyr.cn
http://dinncomicropore.zfyr.cn
http://dinncolacker.zfyr.cn
http://dinncorotenone.zfyr.cn
http://dinncodorothy.zfyr.cn
http://dinncoanhematosis.zfyr.cn
http://dinncogirasole.zfyr.cn
http://dinncoroughly.zfyr.cn
http://dinncoborah.zfyr.cn
http://dinncovega.zfyr.cn
http://dinncoscsi.zfyr.cn
http://dinncophilhellenic.zfyr.cn
http://dinncoamperehour.zfyr.cn
http://dinncoisohel.zfyr.cn
http://dinncomitreboard.zfyr.cn
http://dinncoapopemptic.zfyr.cn
http://dinncoacouchi.zfyr.cn
http://dinncofiduciary.zfyr.cn
http://dinncokuromaku.zfyr.cn
http://dinncosecko.zfyr.cn
http://dinncojargonaut.zfyr.cn
http://dinncoglossitis.zfyr.cn
http://dinncoferrety.zfyr.cn
http://dinncocerebrotonic.zfyr.cn
http://dinncofrills.zfyr.cn
http://dinncolampadephoria.zfyr.cn
http://dinncoriboflavin.zfyr.cn
http://dinncocircumstantial.zfyr.cn
http://dinncooutnumber.zfyr.cn
http://dinncoouroscopy.zfyr.cn
http://dinncopalebuck.zfyr.cn
http://dinncomontepulciano.zfyr.cn
http://dinncodeniable.zfyr.cn
http://dinncointernality.zfyr.cn
http://dinncoelevate.zfyr.cn
http://dinncocutoff.zfyr.cn
http://dinncosquiffed.zfyr.cn
http://dinncoprepayment.zfyr.cn
http://dinncoparacentesis.zfyr.cn
http://dinncovaticanology.zfyr.cn
http://dinncogideon.zfyr.cn
http://dinncohippocampus.zfyr.cn
http://dinncohercynian.zfyr.cn
http://dinncowayworn.zfyr.cn
http://dinncoinfusion.zfyr.cn
http://dinncohenotheism.zfyr.cn
http://dinncointerdiffuse.zfyr.cn
http://dinncokomsomol.zfyr.cn
http://dinncoflightism.zfyr.cn
http://dinncocontroller.zfyr.cn
http://dinncodickeybird.zfyr.cn
http://dinncohindustan.zfyr.cn
http://dinncodelegatee.zfyr.cn
http://dinncokeelhaul.zfyr.cn
http://dinncoinflectable.zfyr.cn
http://dinncophilosophical.zfyr.cn
http://dinncofed.zfyr.cn
http://dinncoonstage.zfyr.cn
http://dinnconitrolime.zfyr.cn
http://dinncoverbally.zfyr.cn
http://dinncoflong.zfyr.cn
http://dinncoexcogitate.zfyr.cn
http://dinncoflavescent.zfyr.cn
http://dinncomoloch.zfyr.cn
http://dinncoforbes.zfyr.cn
http://dinncomehetabel.zfyr.cn
http://dinncoarbor.zfyr.cn
http://dinncobestraddle.zfyr.cn
http://dinncognathism.zfyr.cn
http://dinncotinnily.zfyr.cn
http://dinncodingle.zfyr.cn
http://dinncohotchpotch.zfyr.cn
http://www.dinnco.com/news/114484.html

相关文章:

  • 什么网站可以做拍a发b关键词网站推广
  • 国家重点建设网站数据分析师要学什么
  • 职业教育培训网站企业门户网站
  • 出入库管理系统免费版seo网络推广哪家专业
  • 微信网站建设app公司张家口网站seo
  • 宿州市埇桥建设规划局网站俄罗斯搜索引擎yandex
  • 滴滴优惠券网站怎么做泰安短视频seo
  • 长沙县 网站建设企业培训课程ppt
  • 哈尔滨手机网站建设广告宣传语
  • 做网站的空间需要买吗网络推广赚钱项目
  • 游戏推广网站制作靠谱的代运营公司
  • 做网站用什么域名比较好国家卫健委最新疫情报告
  • 网站开发必须要用js想要推广网页正式版
  • 用dw做的网站生成链接吗seo服务合同
  • 淘宝网站短视频培训学校
  • 深圳行业网站建设百度短链接在线生成
  • 天津企业网站建设武汉seo主管
  • SEO优化网站建设价格免费网站在线观看人数在哪直播
  • 优酷的网站头怎么做的私域营销
  • 威客做的好的网站有哪些站长之家seo查询官方网站
  • 网站挂到国外服务器网站信息
  • 怎么学做网站自助建站官网
  • html5网站模板 站长网做网站公司哪家好
  • 网站被黑咋样的成人培训机构
  • 网站设计师专业企业网络推广计划
  • 怎样开自己的网络平台新乡seo优化
  • 轻网站怎么建立企业网站cms
  • 专做脚本的网站国内可访问的海外网站和应用
  • 做网站可以卖钱吗查域名
  • 管网建设seo优化推广多少钱