首页 Java语言Java多线程一线程启动线程中断

Java语言Java多线程一线程启动线程中断

举报
开通vip

Java语言Java多线程一线程启动线程中断-.z.【Java语言】Java多线程一(线程启动|线程中断)一.线程启动线程启动:--1.继承Thread运行线程:重写Thread类的run方法,然后执行该线程;--2.实现Runnable接口,并运行线程;--代码例如:[java]viewplaincopy在CODE上查看代码片派生到我的代码片package.hanshuliang.thread;publicclassThreadStart{publicstaticvoidmain(String[]args){//1.继承Thread运行线程MyThread...

Java语言Java多线程一线程启动线程中断
-.z.【Java语言】Java多线程一(线程启动|线程中断)一.线程启动线程启动:--1.继承Thread运行线程:重写Thread类的run 方法 快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载 ,然后执行该线程;--2.实现Runnable接口,并运行线程;--代码例如:[java]viewplaincopy在CODE上查看代码片派生到我的代码片package.hanshuliang.thread;publicclassThreadStart{publicstaticvoidmain(String[]args){//1.继承Thread运行线程MyThreadthread=newMyThread();thread.start();//2.实现Runnable接口,并运行线程ThreadrunnableThread=newThread(newMyRunnable());runnableThread.start();}//1.继承Thread类staticclassMyThreade*tendsThread{Overridepublicvoidrun(){super.run();System.out.println("MyThread线程启动");}}//2.实现Runnable接口staticclassMyRunnableimplementsRunnable{Overridepublicvoidrun(){System.out.println("MyRunnable线程启动");}}}--运行结果:[java]viewplaincopy在CODE上查看代码片派生到我的代码片MyThread线程启动MyRunnable线程启动三.线程停顿线程停顿常用方法:--1.使用interrupt()方法停顿线程;--2.使用退出标志,让线程正常退出;--3.弃用的方法(不推荐):使用stop()方法强制停顿线程,但是该方法已经作废,不建议使用;1.使用interrupt()方法停顿线程(1)线程无法立即停顿interrupt()使用 说明 关于失联党员情况说明岗位说明总经理岗位说明书会计岗位说明书行政主管岗位说明书 :--打标记:调用该方法,不能马上停顿该线程,只是在当前线程打了一个停顿标记;代码例如:--代码:[java]viewplaincopy在CODE上查看代码片派生到我的代码片publicclassInterruptDemo{publicstaticclassMyThreade*tendsThread{Overridepublicvoidrun(){super.run();for(inti=1;i<=1000000;i++)//打印一百万数字,大概持续5~10秒左右System.out.println(i);}}publicstaticvoidmain(String[]args)throwsInterruptedE*ception{MyThreadthread=newMyThread();thread.start();//启动线程Thread.sleep(100);//启动线程100ms后中断线程thread.interrupt();//中断线程}}--运行结果:这里只贴上最后几行命令行的运行结果;[plain]viewplaincopy在CODE上查看代码片派生到我的代码片......9999969999979999989999991000000-- 总结 初级经济法重点总结下载党员个人总结TXt高中句型全总结.doc高中句型全总结.doc理论力学知识点总结pdf :在上述程序中,打印了100万数字,从1到1000000,整个过程持续了10秒左右,但是我们在线程开场后100ms就中断了线程,但是线程还是执行完毕了,说明线程并没有在调用interrupt()方法后立即停顿;(2)线程停顿状态判定两个线程停顿状态判定的方法:--1.interrupted()方法:①判断当前线程的中断标志,②如果是中断标志true,则去除中断标志,改为false;,③连续两次调用该方法,第二次返回false,④静态方法:该方法是测试当前线程的中断标志,在哪个线程中调用,就是判定的哪个线程的中断标志,不管调用的主体是哪个线程;--2.isInterrupted()方法:判断线程的中断状态,不管真实的运行状态,只关心状态;--注意:两个方法都是判断中断状态的,与线程的真实运行状况无关;(3)interrupted()方法测试interrupted()方法测试1:测试interrupted方法的判断是否已经中断的效果;--测试代码:[java]viewplaincopy在CODE上查看代码片派生到我的代码片publicclassInterruptedDemo1{publicstaticclassMyThreade*tendsThread{Overridepublicvoidrun(){super.run();for(inti=1;i<=10;i++)//打印10个数字System.out.println(i);}}publicstaticvoidmain(String[]args)throwsInterruptedE*ception{MyThreadthread=newMyThread();thread.start();//启动线程thread.interrupt();//启动线程后马上中断线程System.out.println("第一次:thread.interrupted()="+thread.interrupted());System.out.println("第二次:thread.interrupted()="+thread.interrupted());}}--执行结果:[plain]viewplaincopy在CODE上查看代码片派生到我的代码片第一次:thread.interrupted()=false12第二次:thread.interrupted()=false345678910--总结:启动线程后,立即调用interrupt方法中断线程,但是在主线程中调用thread.Interrupted()方法,打印出来的是主线程的中断状态标志,虽然是调用的thread子线程的对象的方法,但是该方法是一个静态方法,在哪个线程调用,就是打印哪个线程的中断标志;interrupted()方法测试2:测试interrupted方法的去除中断状态的效果;--1.测试代码:[java]viewplaincopy在CODE上查看代码片派生到我的代码片publicclassInterruptedDemo{publicstaticvoidmain(String[]args)throwsInterruptedE*ception{Thread.currentThread().interrupt();//中断主线程System.out.println("第一次:thread.interrupted()="+Thread.interrupted());System.out.println("第二次:thread.interrupted()="+Thread.interrupted());}}--2.执行结果:[java]viewplaincopy在CODE上查看代码片派生到我的代码片第一次:thread.interrupted()=true第二次:thread.interrupted()=false--3.总结:使用interrupted()方法,如果当前线程的状态是中断状态,即返回了true,则需要去除该标志,下一次调用interrupted()方法后,返回值就是false了;(4)isInterrupted()方法测试isInterrupted()方法测试1:测试其中断状态,与上面的interrupted()方法比照;--1.测试代码:[java]viewplaincopy在CODE上查看代码片派生到我的代码片publicclassIsInterruptedDemo{publicstaticclassMyThreade*tendsThread{Overridepublicvoidrun(){super.run();for(inti=1;i<=10;i++)//打印10个数字System.out.println(i);}}publicstaticvoidmain(String[]args)throwsInterruptedE*ception{MyThreadthread=newMyThread();thread.start();//启动线程thread.interrupt();//启动线程后马上中断线程System.out.println("第一次:thread.interrupted()="+thread.isInterrupted());System.out.println("第二次:thread.interrupted()="+thread.isInterrupted());}}--2.返回值:[java]viewplaincopy在CODE上查看代码片派生到我的代码片第一次:thread.interrupted()=true12第二次:thread.interrupted()=true345678910--3.总结分析:isInterrupted()方法只判断被调用对象的该对象线程的线程的中断状态,不管线程的真实运行状况,即使当前线程正在运行,但是线程调用了interrupt()方法,其中断状态为true,因此isInterrupted()方法的返回值一直是true;--4.比照interrupted()方法:interrupted()方法反响的是真实的线程运行状态,线程正在运行,则返回false,如果线程没有运行,返回true;--5.比照Interrupted()方法(静态与普通方法):isInterrupted方法是非静态方法,哪个对象调用返回的就是哪个对象的中断状态;interrupted是静态方法,在哪个线程调用就是返回哪个线程的中断状态;2.异常法停顿线程(1)线程循环中正常退出停顿退出方法:正常退出线程;--1.前提:线程中执行一个循环;--2.中断线程:执行线程中断操作,调用线程的interrupt()方法;--3.查询中断标志:在线程过调用interrupted方法,查询当前的线程中断标志,之后该方法就会将中断标志去除;--4.退出循环:如果查询到中断标志后,直接使用break退出循环;--5.弊端:在线程中,线程没有真正的停顿,线程还是完整的执行了;线程正常退出代码例如:--1.代码:[java]viewplaincopy在CODE上查看代码片派生到我的代码片publicclassE*ceptionInterruptDeo{publicstaticclassMyThreade*tendsThread{Overridepublicvoidrun(){super.run();for(inti=0;i<500;i++){if(interrupted()){//判断线程的中断状态,如果中断直接breakSystem.out.println("停顿状态,退出");break;}System.out.println(i);}System.out.println("MyThread线程执行完毕");//线程完毕标志}}publicstaticvoidmain(String[]args)throwsInterruptedE*ception{MyThreadthread=newMyThread();//创立线程并执行thread.start();//启动线程Thread.sleep(1);//沉睡1毫秒thread.interrupt();//中断线程System.out.println("主线程执行完毕");//判断主线程停顿}}--2.执行结果:[java]viewplaincopy在CODE上查看代码片派生到我的代码片......5051525354主线程执行完毕停顿状态,退出MyThread线程执行完毕--3.总结分析:在线程中调用interrupted()方法,查询中断标志(查询后立即去除中断标志),弊端是停顿线程后,线程还是继续执行后面的逻辑,继续执行完毕,自动退出的;(2)异常退出线程异常法退出线程:通过抛出一个异常,来终止线程执行;--1.前提:线程中执行一个循环;--2.中断线程:执行线程中断操作,调用线程的interrupt()方法;--3.查询中断标志:在线程过调用interrupted方法,查询当前的线程中断标志,之后该方法就会将中断标志去除;--4.抛出异常退出循环:如果查询到中断标志后,直接抛出一个InterruptE*ception异常;--5.捕获处理异常:要将整个run方法中的容使用trycatch代码块捕获,因因为异常捕获后还会继续处理trycatch之后的代码逻辑,如果trycatch代码块之后还有代码逻辑,程序还会执行这段代码;--6.好处:可以自由控制要中断哪些逻辑;异常捕获规则:--1.执行逻辑:捕获异常后,进展异常处理,然后会继续执行trycatch代码块后面的代码逻辑;--2.异常退出围可控:可以自由控制中断哪些操作,继续执行哪些操作;代码测试:--1.代码:[java]viewplaincopy在CODE上查看代码片派生到我的代码片publicclassE*ceptionInterruptDeo{publicstaticclassMyThreade*tendsThread{Overridepublicvoidrun(){try{super.run();for(inti=0;i<500;i++){if(interrupted()){//判断线程的中断状态,如果中断直接breakSystem.out.println("停顿状态,抛出异常退出");thrownewInterruptedE*ception();}//中断标志判定完毕System.out.println(i);}//for循环完毕System.out.println("MyThread线程执行完毕");//线程完毕标志}catch(InterruptedE*ceptione){System.out.println("线程中捕获异常代码块");e.printStackTrace();}//trycatch代码块}//run方法完毕}//线程完毕publicstaticvoidmain(String[]args)throwsInterruptedE*ception{MyThreadthread=newMyThread();//创立线程并执行thread.start();//启动线程Thread.sleep(1);//沉睡1毫秒thread.interrupt();//中断线程System.out.println("主线程执行完毕");//判断主线程停顿}}--2.执行结果:[java]viewplaincopy在CODE上查看代码片派生到我的代码片113114115116主线程执行完毕停顿状态,抛出异常退出线程中捕获异常代码块java.lang.InterruptedE*ceptionatbase.E*ceptionInterruptDeo$MyThread.run(E*ceptionInterruptDeo.java:12)--3.总结分析:在run方法中将整个代码逻辑使用trycatch代码块包裹,异常法只能中断trycatch代码块中的逻辑;3.sleep()中停顿线程(1)先沉睡在终止线程先sleep()再interrupt():先沉睡,再终止线程,线程直接就停顿了;代码例如:--1.代码:[java]viewplaincopy在CODE上查看代码片派生到我的代码片publicclassSleepInterruptDemo{publicstaticclassMyThreade*tendsThread{Overridepublicvoidrun(){try{System.out.println("线程逻辑开场");super.run();sleep(1000);//启动后立即进入沉睡状态,沉睡1000msSystem.out.println("线程逻辑完毕");}catch(InterruptedE*ceptione){System.out.println("捕获到了异常,进入了catch代码块");e.printStackTrace();}//catch代码块System.out.println("run方法完毕");}//run方法}//线程publicstaticvoidmain(String[]args)throwsInterruptedE*ception{MyThreadthread=newMyThread();//新建线程thread.start();//线程启动Thread.sleep(100);//沉睡100毫秒,线程中thread.interrupt();//中断线程}}--2.执行结果:[java]viewplaincopy在CODE上查看代码片派生到我的代码片线程逻辑开场捕获到了异常,进入了catch代码块run方法完毕java.lang.InterruptedE*ception:sleepinterruptedatjava.lang.Thread.sleep(NativeMethod)atbase.SleepInterruptDemo$MyThread.run(SleepInterruptDemo.java:11)--3.总结分析:在沉睡状态下,如果调用interrupt()方法,线程中会直接抛出InterruptE*ception异常;(2)先终止线程再沉睡先终止线程再sleep:先终止线程,在sleep,则sleep之前的代码需要实现相关逻辑代码例如:--1.代码:与上面的区别是在sleep之前有一段循环逻辑;[java]viewplaincopy在CODE上查看代码片派生到我的代码片publicclassSleepInterruptDemo{publicstaticclassMyThreade*tendsThread{Overridepublicvoidrun(){try{System.out.println("线程逻辑开场");super.run();for(inti=0;i<500;i++)System.out.println(i);sleep(1000);//启动后立即进入沉睡状态,沉睡1000msSystem.out.println("线程逻辑完毕");}catch(InterruptedE*ceptione){System.out.println("捕获到了异常,进入了catch代码块");e.printStackTrace();}//catch代码块System.out.println("run方法完毕");}//run方法}//线程publicstaticvoidmain(String[]args)throwsInterruptedE*ception{MyThreadthread=newMyThread();//新建线程thread.start();//线程启动thread.interrupt();//中断线程System.out.println("主线程中断线程");}}--2.执行结果:[java]viewplaincopy在CODE上查看代码片派生到我的代码片496497498499捕获到了异常,进入了catch代码块run方法完毕java.lang.InterruptedE*ception:sleepinterruptedatjava.lang.Thread.sleep(NativeMethod)atbase.SleepInterruptDemo$MyThread.run(SleepInterruptDemo.java:12)--3.总结:使用Interrupt方法后,如果正在执行循环,就不会抛异常退出线程,进入sleep状态后,会立即抛出异常,退出线程;4.stop()停顿线程(1)stop方法停顿线程的效果stop停顿线程:--1.立即停顿:调用stop()方法停顿线程,比拟暴力,会立即停顿当前的线程;--2.抛出异常:使用stop()方法停顿线程会抛出一个ThreadDeath异常,这个异常可以不捕捉;--3.适用场景:适用该方法停顿线程,前提示线程的相关数据和线程本身都不再使用了,否则会造成数据混乱;stop()停顿线程效果演示:--1.代码例如:[java]viewplaincopy在CODE上查看代码片派生到我的代码片publiccsm136lassStopDemo{publicstaticclassMyThreade*tendsThread{Overridepublicvoidrun(){try{System.out.println("线程逻辑开场");super.run();for(inti=0;i<5000;i++){System.out.println(i);sleep(100);}System.out.println("线程逻辑完毕");}catch(InterruptedE*ceptione){System.out.println("捕获到了InterruptedE*ception异常,进入了catch代码块");e.printStackTrace();}//catch代码块System.out.println("run方法完毕");}//run方法}//线程publicstaticvoidmain(String[]args)throwsInterruptedE*ception{MyThreadthread=newMyThread();//新建线程thread.start();//线程启动Thread.sleep(500);//沉睡500ms,让线程打印5个数字thread.stop();//中断线程System.out.println("主线程中断线程");}}--2.运行结果:[java]viewplaincopy在CODE上查看代码片派生到我的代码片线程逻辑开场01234主线程中断线程--3.总结分析:线程直接中断了,线程中run()方法的最后一行代码也没有执行,循环逻辑完毕也没有执行,说明线程很暴力的直接退出,没有任何处理;(2)stop方法停顿线程捕获ThreadDeath异常关于异常捕捉:--1.捕捉ThreadDeath异常:线程捕获异常处理后,还会继续执行trycatch代码块下面的代码;--2.不捕捉ThreadDeath异常:线程直接从stop时刻退出,不会执行下面的代码;stop()停顿线程并捕获异常效果演示:--1.代码例如:代码中比上面多了一个catchThreadDeath异常的代码块,其它一样;[java]viewplaincopy在CODE上查看代码片派生到我的代码片publicclassStopDemo{publicstaticclassMyThreade*tendsThread{Overridepublicvoidrun(){try{System.out.println("线程逻辑开场");super.run();for(inti=0;i<5000;i++){System.out.println(i);sleep(100);}System.out.println("线程逻辑完毕");}catch(InterruptedE*ceptione){System.out.println("捕获到了InterruptedE*ception异常,进入了catch代码块");e.printStackTrace();}catch(ThreadDeathe){System.out.println("捕获到了ThreadDeath异常,进入了catch代码块");e.printStackTrace();}//catch代码块System.out.println("run方法完毕");}//run方法}//线程publicstaticvoidmain(String[]args)throwsInterruptedE*ception{MyThreadthread=newMyThread();//新建线程thread.start();//线程启动Thread.sleep(500);//沉睡500ms,让线程打印5个数字thread.stop();//中断线程System.out.println("主线程中断线程");}}--2.运行结果:[java]viewplaincopy在CODE上查看代码片派生到我的代码片线程逻辑开场01234主线程中断线程捕获到了ThreadDeath异常,进入了catch代码块run方法完毕java.lang.ThreadDeathatjava.lang.Thread.stop(UnknownSource)at.hanshuliang.thread.StopDemo.main(StopDemo.java:31)--3.总结分析:如果捕获了ThreadDeath异常,就会处理这个异常,并会执行异常处理后面的代码,run()方法的最后一行代码也执行完毕了;5.return停顿线程return停顿线程说明:--1.执行过程:线程运行中,随时监测中断标记,如果检测到中断标记后,直接return退出run方法;--2.不建议使用该方法,多个return会污染代码;return退出演示:--1.代码例如:[java]viewplaincopy在CODE上查看代码片派生到我的代码片publicc630lassReturnDemo{publicstaticclassMyThreade*tendsThread{Overridepublicvoidrun(){super.run();for(inti=0;i<500;i++){if(interrupted()){//判断线程的中断状态,如果中断直接breakSystem.out.println("停顿状态,return退出");return;}System.out.println(i);}System.out.println("MyThread线程执行完毕");//线程完毕标志}}publicstaticvoidmain(String[]args)throwsInterruptedE*ception{MyThreadthread=newMyThread();//创立线程并执行thread.start();//启动线程Thread.sleep(1);//沉睡1毫秒thread.interrupt();//中断线程System.out.println("主线程执行完毕");//判断主线程停顿}}--2.执行结果:[java]viewplaincopy在CODE上查看代码片派生到我的代码片......3536373839主线程执行完毕停顿状态,return退出--3.总结分析:使用return直接退出run方法,确实实现了立即停顿线程的目的,但是我们还是建议使用异常法控制线程停顿;
本文档为【Java语言Java多线程一线程启动线程中断】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
个人认证用户
dykcs64
从事建筑工程对接,工程图纸设计施工管理方面的经验
格式:doc
大小:64KB
软件:Word
页数:24
分类:教育学
上传时间:2022-07-31
浏览量:4