当前位置:首页 » 编程语言 » java关闭线程

java关闭线程

发布时间: 2022-04-28 11:36:50

A. java怎么强制关闭一个正在运行的线程

线程运行中加入sleep时间 ,然后调用该线程的interrupt()方法就可以了。

MyThreadmt=newMyThread();
Threadt=newThread(mt);
System.out.println("Systemisreadytostartthread");
t.start();

Thread.sleep(3000);

System.out.println("Systemisreadytostopthread");
//线程没有处于阻塞状态,调用线程对应的interrupt()不能让运行的线程停止下来
t.interrupt();

B. java thread.start是线程开始 怎么关闭线程呢

1. 使用退出标志,使线程正常退出,也就是当run方法完成后线程终止。
2. 使用stop方法强行终止线程(这个方法不推荐使用,因为stop和suspend、resume一样,也可能发生不可预料的结果)。
3. 使用interrupt方法中断线程。

C. java线程如何停止

通过调用interrupt方法可以使得处于阻塞状态的线程抛出一个异常,即interrupt方法可以用来中断一个正处于阻塞状态的线程;另外,改方法还会设置线程的中断状态(注:isInterrupted()可以用来查询中断状态)。

D. java线程如何停止

终止线程的三种方法:
1. 使用退出标志,使线程正常退出,也就是当run方法完成后线程终止。
2. 使用stop方法强行终止线程(这个方法不推荐使用,因为stop和suspend、resume一样,也可能发生不可预料的结果)。
3. 使用interrupt方法中断线程。

1. 使用退出标志终止线程
当run方法执行完后,线程就会退出。但有时run方法是永远不会结束的。如在服务端程序中使用线程进行监听客户端请求,或是其他的需要循环处理的任务。在这种情况下,一般是将这些任务放在一个循环中,如while循环。如果想让循环永远运行下去,可以使用while(true){……}来处理。但要想使while循环在某一特定条件下退出,最直接的方法就是设一个boolean类型的标志,并通过设置这个标志为true或false来控制while循环是否退出。下面给出了一个利用退出标志终止线程的例子。

packagechapter2;

{
publicvolatilebooleanexit=false;
publicvoidrun()
{
while(!exit);
}
publicstaticvoidmain(String[]args)throwsException
{
ThreadFlagthread=newThreadFlag();
thread.start();
sleep(5000);//主线程延迟5秒
thread.exit=true;//终止线程thread
thread.join();
System.out.println("线程退出!");
}
}

在上面代码中定义了一个退出标志exit,当exit为true时,while循环退出,exit的默认值为false.在定义exit时,使用了一个Java关键字volatile,这个关键字的目的是使exit同步,也就是说在同一时刻只能由一个线程来修改exit的值,

2. 使用stop方法终止线程
使用stop方法可以强行终止正在运行或挂起的线程。我们可以使用如下的代码来终止线程:
thread.stop();
虽然使用上面的代码可以终止线程,但使用stop方法是很危险的,就象突然关闭计算机电源,而不是按正常程序关机一样,可能会产生不可预料的结果,因此,并不推荐使用stop方法来终止线程。

3. 使用interrupt方法终止线程
使用interrupt方法来终端线程可分为两种情况:
(1)线程处于阻塞状态,如使用了sleep方法。
(2)使用while(!isInterrupted()){……}来判断线程是否被中断。
在第一种情况下使用interrupt方法,sleep方法将抛出一个InterruptedException例外,而在第二种情况下线程将直接退出。下面的代码演示了在第一种情况下使用interrupt方法。

packagechapter2;

{
publicvoidrun()
{
try
{
sleep(50000);//延迟50秒
}
catch(InterruptedExceptione)
{
System.out.println(e.getMessage());
}
}
publicstaticvoidmain(String[]args)throwsException
{
Threadthread=newThreadInterrupt();
thread.start();
System.out.println("在50秒之内按任意键中断线程!");
System.in.read();
thread.interrupt();
thread.join();
System.out.println("线程已经退出!");
}
}

上面代码的运行结果如下:
在50秒之内按任意键中断线程!
sleep interrupted
线程已经退出!
在调用interrupt方法后, sleep方法抛出异常,然后输出错误信息:sleep interrupted.
注意:在Thread类中有两个方法可以判断线程是否通过interrupt方法被终止。一个是静态的方法interrupted(),一个是非静态的方法isInterrupted(),这两个方法的区别是interrupted用来判断当前线是否被中断,而isInterrupted可以用来判断其他线程是否被中断。因此,while (!isInterrupted())也可以换成while (!Thread.interrupted())。

E. java如何关闭线程

关闭线程有几种方法,
一种是调用它里面的stop()方法
另一种就是你自己设置一个停止线程的标记 (推荐这种)
代码如下:
package com.demo;
//测试Thread的stop方法和自己编写一个停止标记来停止线程;
public class StopThread implements Runnable{
//停止线程的标记值boolean;
private boolean flag = true;
public void stopThread(){
flag = false;
}
public void run(){
int i=0;
while(flag){
i++;
System.out.println(Thread.currentThread().getName()+":"+i);
try{
Thread.sleep(1000);
}catch(Exception e){
}
System.out.println(Thread.currentThread().getName()+"==>"+i);
}
}
public static void main(String args[]){
StopThread st = new StopThread();
Thread th = new Thread(st);
Thread th1 = new Thread(st);
th.start();
th1.start();
try{
Thread.sleep(5500);
}catch(Exception e){
}
/*
如果使用Thread.stop方法停止线程,不能保证这个线程是否完整的运行完成一次
run方法;但是如果使用停止的标记位,那么可以保正在真正停止之前完整的运行完
成一次run方法;
*/
th.stop();
st.stopThread();
}
}

F. 如何关闭java无用线程

当用完某个线程后,在结束时用(线程名)thread.close();
及时关闭不用线程可以避免资源浪费

G. java 怎么强制关闭 一个线程

在Java的多线程编程中,java.lang.Thread类型包含了一些列的方法start(), stop(), stop(Throwable) and suspend(), destroy() and resume()。通过这些方法,我们可以对线程进行方便的操作,但是这些方法中,只有start()方法得到了保留。
在Sun公司的一篇文章《Why are Thread.stop, Thread.suspend and Thread.resume Deprecated? 》中详细讲解了舍弃这些方法的原因。
如果真的需要终止一个线程,可以使用以下几种方法:
1、让线程的run()方法执行完,线程自然结束。(这种方法最好)

2、通过轮询和共享标志位的方法来结束线程,例如while(flag){},flag的初始值设为真,当需要结束时,将flag的值设为false。(这种方法也不很好,因为如果while(flag){}方法阻塞了,则flag会失效)
如果线程因为执行sleep()或是wait()而进入Not Runnable状态,假如是wait() 用标志位就方法就不行了,
public final void wait(long timeout)
throws InterruptedException此方法导致当前线程(称之为 T)将其自身放置在对象的等待集中,然后放弃此对象上的所有同步要求。即当前线程变为等待状态
wait() 的标准使用方法
synchronized(obj){
while(<不满足条件>){
obj.wait();
}
满足条件的处理过程
}
而您想要停止它,您可以使用第三种即
3 使用interrupt(),而程式会丢出InterruptedException例外,因而使得执行绪离开run()方法

H. 如何优雅的关闭java线程

Java中终止线程的方式主要有三种:

1、使用stop()方法,已被弃用。原因是:stop()是立即终止,会导致一些数据被到处理一部分就会被终止,而用户并不知道哪些数据被处理,哪些没有被处理,产生了不完整的“残疾”数据,不符合完整性,所以被废弃。So, forget it!

2、使用volatile标志位

看一个简单的例子:

首先,实现一个Runnable接口,在其中定义volatile标志位,在run()方法中使用标志位控制程序运行:

{

//定义退出标志,true会一直执行,false会退出循环
//使用volatile目的是保证可见性,一处修改了标志,处处都要去主存读取新的值,而不是使用缓存
publicvolatilebooleanflag=true;

publicvoidrun(){
System.out.println("第"+Thread.currentThread().getName()+"个线程创建");

try{
Thread.sleep(1000L);
}catch(InterruptedExceptione){
e.printStackTrace();
}

//退出标志生效位置
while(flag){
}
System.out.println("第"+Thread.currentThread().getName()+"个线程终止");
}
}

然后,在main()方法中创建线程,在合适的时候,修改标志位,终止运行中的线程。

publicclassTreadTest{
publicstaticvoidmain(String[]arg)throwsInterruptedException{
MyRunnablerunnable=newMyRunnable();

//创建3个线程
for(inti=1;i<=3;i++){
Threadthread=newThread(runnable,i+"");
thread.start();
}
//线程休眠
Thread.sleep(2000L);
System.out.println("——————————————————————————");
//修改退出标志,使线程终止
runnable.flag=false;
}
}

最后,运行结果,如下:

第1个线程创建
第2个线程创建
第3个线程创建
--------------------------
第2个线程终止
第1个线程终止
第3个线程终止

3、使用interrupt()中断的方式,注意使用interrupt()方法中断正在运行中的线程只会修改中断状态位,可以通过isInterrupted()判断。如果使用interrupt()方法中断阻塞中的线程,那么就会抛出InterruptedException异常,可以通过catch捕获异常,然后进行处理后终止线程。有些情况,我们不能判断线程的状态,所以使用interrupt()方法时一定要慎重考虑。

答案来源于我的另一个回答:https://..com/question/940530399412929252。

I. 如何正确结束Java线程

1. 使用退出标志,使线程正常退出,也就是当run方法完成后线程终止。
2. 使用stop方法强行终止线程(这个方法不推荐使用,因为stop和suspend、resume一样,也可能发生不可预料的结果)。
3. 使用interrupt方法中断线程。

1. 使用退出标志终止线程
当run方法执行完后,线程就会退出。但有时run方法是永远不会结束的。如在服务端程序中使用线程进行监听客户端请求,或是其他的需要循环处理的任务。在这种情况下,一般是将这些任务放在一个循环中,如while循环。如果想让循环永远运行下去,可以使用while(true){……}来处理。但要想使while循环在某一特定条件下退出,最直接的方法就是设一个boolean类型的标志,并通过设置这个标志为true或false来控制while循环是否退出。下面给出了一个利用退出标志终止线程的例子。
package chapter2;
public class ThreadFlag extends Thread
{
public volatile boolean exit = false;
public void run()
{
while (!exit);
}
public static void main(String[] args) throws Exception
{
ThreadFlag thread = new ThreadFlag();
thread.start();
sleep(5000); // 主线程延迟5秒
thread.exit = true; // 终止线程thread
thread.join();
System.out.println("线程退出!");
}
}
在上面代码中定义了一个退出标志exit,当exit为true时,while循环退出,exit的默认值为false.在定义exit时,使用了一个Java关键字volatile,这个关键字的目的是使exit同步,也就是说在同一时刻只能由一个线程来修改exit的值,

2. 使用stop方法终止线程
使用stop方法可以强行终止正在运行或挂起的线程。我们可以使用如下的代码来终止线程:
thread.stop();
虽然使用上面的代码可以终止线程,但使用stop方法是很危险的,就象突然关闭计算机电源,而不是按正常程序关机一样,可能会产生不可预料的结果,因此,并不推荐使用stop方法来终止线程。

3. 使用interrupt方法终止线程
使用interrupt方法来终端线程可分为两种情况:
(1)线程处于阻塞状态,如使用了sleep方法。
(2)使用while(!isInterrupted()){……}来判断线程是否被中断。
在第一种情况下使用interrupt方法,sleep方法将抛出一个InterruptedException例外,而在第二种情况下线程将直接退出。下面的代码演示了在第一种情况下使用interrupt方法。
package chapter2;
public class ThreadInterrupt extends Thread
{
public void run()
{
try
{
sleep(50000); // 延迟50秒
}
catch (InterruptedException e)
{
System.out.println(e.getMessage());
}
}
public static void main(String[] args) throws Exception
{
Thread thread = new ThreadInterrupt();
thread.start();
System.out.println("在50秒之内按任意键中断线程!");
System.in.read();
thread.interrupt();
thread.join();
System.out.println("线程已经退出!");
}
}
上面代码的运行结果如下:
在50秒之内按任意键中断线程!
sleep interrupted
线程已经退出!
在调用interrupt方法后, sleep方法抛出异常,然后输出错误信息:sleep interrupted.
注意:在Thread类中有两个方法可以判断线程是否通过interrupt方法被终止。一个是静态的方法interrupted(),一个是非静态的方法isInterrupted(),这两个方法的区别是interrupted用来判断当前线是否被中断,而isInterrupted可以用来判断其他线程是否被中断。因此,while (!isInterrupted())也可以换成while (!Thread.interrupted())。

J. 如何关闭java线程

终止线程的三种方法
1. 使用退出标志,使线程正常退出,也就是当run方法完成后线程终止。
2. 使用stop方法强行终止线程(这个方法不推荐使用,因为stop和suspend、resume一样,也可能发生不可预料的结果)。
3. 使用interrupt方法中断线程。

1. 使用退出标志终止线程
当run方法执行完后,线程就会退出。但有时run方法是永远不会结束的。如在服务端程序中使用线程进行监听客户端请求,或是其他的需要循环处理的任务。在这种情况下,一般是将这些任务放在一个循环中,如while循环。如果想让循环永远运行下去,可以使用while(true){……}来处理。但要想使while循环在某一特定条件下退出,最直接的方法就是设一个boolean类型的标志,并通过设置这个标志为true或false来控制while循环是否退出。下面给出了一个利用退出标志终止线程的例子。
package chapter2;
public class ThreadFlag extends Thread
{
public volatile boolean exit = false;
public void run()
{
while (!exit);
}
public static void main(String[] args) throws Exception
{
ThreadFlag thread = new ThreadFlag();
thread.start();
sleep(5000); // 主线程延迟5秒
thread.exit = true; // 终止线程thread
thread.join();
System.out.println("线程退出!");
}
}
在上面代码中定义了一个退出标志exit,当exit为true时,while循环退出,exit的默认值为false.在定义exit时,使用了一个Java关键字volatile,这个关键字的目的是使exit同步,也就是说在同一时刻只能由一个线程来修改exit的值,

2. 使用stop方法终止线程
使用stop方法可以强行终止正在运行或挂起的线程。我们可以使用如下的代码来终止线程:
thread.stop();
虽然使用上面的代码可以终止线程,但使用stop方法是很危险的,就象突然关闭计算机电源,而不是按正常程序关机一样,可能会产生不可预料的结果,因此,并不推荐使用stop方法来终止线程。

3. 使用interrupt方法终止线程
使用interrupt方法来终端线程可分为两种情况:
(1)线程处于阻塞状态,如使用了sleep方法。
(2)使用while(!isInterrupted()){……}来判断线程是否被中断。
在第一种情况下使用interrupt方法,sleep方法将抛出一个InterruptedException例外,而在第二种情况下线程将直接退出。下面的代码演示了在第一种情况下使用interrupt方法。
package chapter2;
public class ThreadInterrupt extends Thread
{
public void run()
{
try
{
sleep(50000); // 延迟50秒
}
catch (InterruptedException e)
{
System.out.println(e.getMessage());
}
}
public static void main(String[] args) throws Exception
{
Thread thread = new ThreadInterrupt();
thread.start();
System.out.println("在50秒之内按任意键中断线程!");
System.in.read();
thread.interrupt();
thread.join();
System.out.println("线程已经退出!");
}
}
上面代码的运行结果如下:
在50秒之内按任意键中断线程!
sleep interrupted
线程已经退出!
在调用interrupt方法后, sleep方法抛出异常,然后输出错误信息:sleep interrupted.
注意:在Thread类中有两个方法可以判断线程是否通过interrupt方法被终止。一个是静态的方法interrupted(),一个是非静态的方法isInterrupted(),这两个方法的区别是interrupted用来判断当前线是否被中断,而isInterrupted可以用来判断其他线程是否被中断。因此,while (!isInterrupted())也可以换成while (!Thread.interrupted())。

热点内容
邮政登陆密码是什么意思 发布:2025-07-15 01:53:23 浏览:229
算法与程序设计vb 发布:2025-07-15 01:50:39 浏览:719
什么是测试脚本 发布:2025-07-15 01:44:58 浏览:514
商汤科技存储负责人 发布:2025-07-15 01:24:21 浏览:252
文件夹如何批量替换文件名 发布:2025-07-15 01:19:15 浏览:68
ftp上传网页 发布:2025-07-15 01:13:09 浏览:182
音乐文件夹图标 发布:2025-07-15 01:03:41 浏览:495
安卓机怎么反向充电 发布:2025-07-15 01:03:40 浏览:501
电脑使用华为云服务器 发布:2025-07-15 00:48:10 浏览:534
中考应该如何排解压力 发布:2025-07-15 00:17:54 浏览:363