上海水磨工作室引导语:Java的主要工作是通过编程语言来制作互联网页面、制作动态效果以及网站等技术,以下是百分网小编分享给大家的利用Java终止线程的方法,欢迎阅读!
1、让线程的run()方法执行完,线程自然结束。(这种方法最好)
2、通过轮询和共享标志位的方法来结束线程,例如while(flag){},flag的初始值设为真,当需要结束时,将flag的值设为false。(这种方法也不很好,因为如果while(flag){}方法阻塞了,则flag会失效)
publicclassSomeThreadimplementsRunnable{
上海水磨工作室privatevolatilebooleanstop=false;
上海水磨工作室publicvoidterminate(){
stop=ture;
}
publicvoidrun(){
while(stop){
//...somestatements
}
}
}
上海水磨工作室如果线程因为执行sleep()或是wait()而进入NotRunnable状态,假如是wait()用标志位就方法就不行了,
上海水磨工作室publicfinalvoidwait(longtimeout)
上海水磨工作室throwsInterruptedException
上海水磨工作室此方法导致当前线程(称之为T)将其自身放置在对象的等待集中,然后放弃此对象上的所有同步要求。即当前线程变为等待状态
上海水磨工作室wait()的标准使用方法
上海水磨工作室synchronized(obj){
while(<不满足条件>){
obj.wait();
}
满足条件的处理过程
}
上海水磨工作室而您想要停止它,您可以使用第三种即
3使用interrupt(),而程式会丢出InterruptedException例外,因而使得执行绪离开run()方法,
例如:
上海水磨工作室publicclassSomeThread{
publicstaticvoidmain(String[]args)
{
Threadthread=newThread(newRunnable(){
上海水磨工作室publicvoidrun(){
while(!Thread.interrupted()){
//处理所要处理的工作
try{
System.out.println("gotosleep");
Thread.sleep(1000);
}catch(InterruptedExceptione){
e.printStackTrace();
上海水磨工作室System.out.println("iaminterrupted!");
}
});
thread.start();
上海水磨工作室thread.interrupt();
}
}
执行结果为:
gotosleep
iaminterrupted!
第2篇:JAVA中终止线程的方法
在Java的多线程编程中,java.lang.Thread类型包含了一些列的方法start(),stop(),stop(Throwable)andsuspend(),destroy()andresume()。通过这些方法,我们可以对线程进行方便的*作,但是这些方法中,只有start()方法得到了保留。本文是百分网小编搜索整理的关于JAVA中终止线程的方法,供参考复习,希望对大家有所帮助!想了解更多相关信息请持续关注我们应届毕业生考试网!
上海水磨工作室如果真的需要终止一个线程,可以使用以下几种方法:
上海水磨工作室1、让线程的run()方法执行完,线程自然结束。(这种方法最好)
上海水磨工作室2、通过轮询和共享标志位的方法来结束线程,例如while(flag){},flag的初始值设为真,当需要结束时,将flag的值设为false。(这种方法也不很好,因为如果while(flag){}方法阻塞了,则flag会失效)
代码如下:
publicclassSomeThreadimplementsRunnable{
上海水磨工作室privatevolatilebooleanstop=false;
publicvoidterminate(){
stop=ture;
}
上海水磨工作室publicvoidrun(){
while(stop){
//...somestatements
}
}
}
上海水磨工作室如果线程因为执行sleep()或是wait()而进入NotRunnable状态,假如是wait()用标志位就方法就不行了,
publicfinalvoidwait(longtimeout)
throwsInterruptedException此方法导致当前线程(称之为T)将其自身放置在对象的等待集中,然后放弃此对象上的所有同步要求。即当前线程变为等待状态
wait()的标准使用方法
上海水磨工作室synchronized(obj){
while(<不满足条件>){
obj.wait();
}
满足条件的处理过程
}
而您想要停止它,您可以使用第三种即
上海水磨工作室3使用interrupt(),而程式会丢出InterruptedException例外,因而使得执行绪离开run()方法,
例如:
代码如下:
publicclassSomeThread{
publicstaticvoidmain(String[]args)
{
Threadthread=newThread(newRunnable(){
上海水磨工作室publicvoidrun(){
while(!Thread.interrupted()){
//处理所要处理的工作
try{
System.out.println("gotosleep");
上海水磨工作室Thread.sleep(1000);
}catch(InterruptedExceptione){
上海水磨工作室e.printStackTrace();
System.out.println("iaminterrupted!");
}
});
上海水磨工作室thread.start();
thread.interrupt();
}
}
执行结果为:
gotosleep
iaminterrupted!
第3篇:JAVA中终止线程的方法有哪些
Java的多线程编程中,java.lang.Thread类型包含了一些列的方法start(),stop(),stop(Throwable)andsuspend(),destroy()andresume()。下面是小编为大家带来的JAVA中终止线程的方法,欢迎阅读。
Java的多线程编程中,java.lang.Thread类型包含了一些列的方法start(),stop(),stop(Throwable)andsuspend(),destroy()andresume()。通过这些方法,我们可以对线程进行方便的*作,但是这些方法中,只有start()方法得到了保留。
在Sun公司的一篇文章《WhyareThread.stop,Thread.suspendandThread.resumeDeprecated?》中详细讲解了舍弃这些方法的原因。
上海水磨工作室如果真的需要终止一个线程,可以使用以下几种方法:
1、让线程的run()方法执行完,线程自然结束。(这种方法最好)
2、通过轮询和共享标志位的方法来结束线程,例如while(flag){},flag的初始值设为真,当需要结束时,将flag的值设为false。(这种方法也不很好,因为如果while(flag){}方法阻塞了,则flag会失效)
publicclassSomeThreadimplementsRunnable{
上海水磨工作室privatevolatilebooleanstop=false;
上海水磨工作室publicvoidterminate(){
stop=ture;
}
publicvoidrun(){
上海水磨工作室while(stop){
//...somestatements
}
}
}
上海水磨工作室如果线程因为执行sleep()或是wait()而进入NotRunnable状态,假如是wait()用标志位就方法就不行了,
publicfinalvoidwait(longtimeout)
throwsInterruptedException
此方法导致当前线程(称之为T)将其自身放置在对象的等待集中,然后放弃此对象上的所有同步要求。即当前线程变为等待状态
wait()的标准使用方法
synchronized(obj){
上海水磨工作室while(<不满足条件>){
obj.wait();
}
满足条件的处理过程
}
上海水磨工作室而您想要停止它,您可以使用第三种即
3使用interrupt(),而程式会丢出InterruptedException例外,因而使得执行绪离开run()方法,
例如:
publicclassSomeThread{
publicstaticvoidmain(String[]args)
{
Threadthread=newThread(newRunnable(){
publicvoidrun(){
while(!Thread.interrupted()){
上海水磨工作室//处理所要处理的工作
try{
上海水磨工作室System.out.println("gotosleep");
上海水磨工作室Thread.sleep(1000);
}catch(InterruptedExceptione){
上海水磨工作室e.printStackTrace();
上海水磨工作室System.out.println("iaminterrupted!");
}
});
上海水磨工作室thread.start();
上海水磨工作室thread.interrupt();
}
}
执行结果为:
gotosleep
上海水磨工作室iaminterrupted!