线程停止(stop() interrupt() interrupted() isInterrupted())

stop() 强制停止线程;已经弃用;可能造成数据不一致等问题。

interrupt() 在线程中做停止标记,并非真的停止线程。

this.interrupted()测试当前线程是否中断,并清除中断状态。

this.isInterrupted() 测试线程是否已经中断。

方法声明:

public static boolean interrupted()  静态方法

public boolean isInterrupted()       非静态方法

例子:

package ThreadTest2;

public class MyThread extends Thread{

@Override

public void run() {

// TODO Auto-generated method stub

super.run();

for(int i=0;i<500000;i++){

//         System.out.println(Thread.currentThread().getName());

//         System.out.println(Thread.currentThread().isInterrupted());

System.out.println("i="+(i+1));

}

}

}

package ThreadTest2;

public class Run {

public static void main(String[] args){

try{

MyThread thread=new MyThread();

thread.start();

Thread.sleep(1000);

thread.interrupt();

//当前正在执行的线程是main,因此没有停止

System.out.println("是否停止了1?=" + Thread.interrupted());

System.out.println("是否停止了2?=" + Thread.interrupted());

//终止main方法。

//interrupted()方法会清除线程的中断状态,所以4的时候是false

Thread.currentThread().interrupt();

System.out.println("是否停止了3?=" + Thread.interrupted());

System.out.println("是否停止了4?=" + Thread.interrupted());

}catch(InterruptedException e){

System.out.println("main catch");

e.printStackTrace();

}

System.out.println("end!");

}

}

结果:

i=4997

i=4998

i=4999

i=5000

是否停止了1?=false

是否停止了2?=false

是否停止了3?=true

是否停止了4?=false

end!

package ThreadTest2;

import java.io.FileNotFoundException;

import java.io.PrintStream;

public class Run3 {

public static void main(String[] args) throws Exception{

try{

MyThread thread=new MyThread();

thread.start();

Thread.sleep(1000);

thread.interrupt();

PrintStream ps = new PrintStream("e:/log.txt");

System.setOut(ps);

//当前正在执行的线程是main,因此没有停止

System.out.println(thread.getName() + "是否停止了5?=" + thread.isInterrupted());

System.out.println(thread.getName() + "是否停止了6?=" + thread.isInterrupted());

}catch(InterruptedException e){

System.out.println("main catch");

e.printStackTrace();

}

System.out.println("end!");

}

}

/**

*1  interrupt()并非立即停止线程。run方法还在继续执行,因此说interrupt()只是打了个标记。

*

* */

结果:

。。。。。。

i=126220

Thread-0是否停止了5?=true

i=126221

Thread-0是否停止了6?=true

i=126222

i=126223

以上两个例子,线程都没有停止,仍继续执行。

用interrupt()停止线程。

1 通过抛出异常的方式,中止线程

package ThreadTest3;

public class MyThread extends Thread{

@Override

public void run() {

// TODO Auto-generated method stub

super.run();

try{

for(int i=0;i<500000;i++){

if(this.interrupted()){

System.out.println("线程已停止,准备退出!");

//通过抛出异常的方式,中止线程

throw new InterruptedException();

}

System.out.println("i="+(i+1));

}

System.out.println("for方法之后");

}catch(InterruptedException e){

System.out.println("MyThread类catch方法");

e.printStackTrace();

}

}

}

package ThreadTest3;

public class Run {

public static void main(String[] args){

try{

MyThread thread=new MyThread();

thread.start();

Thread.sleep(2000);

thread.interrupt();

}catch(InterruptedException e){

System.out.println("main catch");

e.printStackTrace();

}

System.out.println("end!");

}

}

结果:

i=256841

i=256842

i=256843

end!

线程已停止,准备退出!

MyThread类catch方法

java.lang.InterruptedException

at ThreadTest3.MyThread.run(MyThread.java:13)

注意:interrupt() 和sleep(),先interrupt再sleep或者先sleep再interrupt都会抛出java.lang.InterruptedException。

stop()强制中止线程。(弃用,造成数据不一致)

package threadTest4;

public class MyThread extends Thread{

private SynchronizedObject object;

public MyThread(SynchronizedObject object){

super();

this.object=object;

}

@Override

public void run() {

object.printStriing("b", "bb");

}

}

package threadTest4;

public class Run {

public static void main(String[] args){

try{

SynchronizedObject object=new SynchronizedObject();

MyThread thread = new MyThread(object);

thread.start();

Thread.sleep(500);

thread.stop();

System.out.println(object.getUsername() + " " +object.getPassword());

}catch(InterruptedException e){

e.printStackTrace();

}

}

}

package threadTest4;

public class SynchronizedObject {

private String username="a";

private String password="aa";

public String getUsername(){

return username;

}

public void setUsername(String username){

this.username=username;

}

public String getPassword(){

return password;

}

public void setPassword(String password){

this.password=password;

}

synchronized public void printStriing(String username,String password){

try{

this.username=username;

Thread.sleep(100000);

this.password=password;

}catch(InterruptedException e){

e.printStackTrace();

}

}

}

结果:

b aa

interrupt() 和 return()结合。

package threadTest5;

public class MyThread extends Thread{

@Override

public void run() {

while(true){

if(this.interrupted()){

System.out.println("线程停止");

return;

}

System.out.println("timer= " + System.currentTimeMillis());

}

}

}

package threadTest5;

public class Run {

public static void main(String[] args){

try{

MyThread thread = new MyThread();

thread.start();

Thread.sleep(2000);

thread.interrupt();

}catch(InterruptedException e){

e.printStackTrace();

}

}

}

结果:

。。。。。。

timer= 1581934919550

timer= 1581934919550

timer= 1581934919550

timer= 1581934919550

线程停止

参考:《Java多线程编程核心技术》 高洪岩

原文地址:https://www.cnblogs.com/perfumeBear/p/12322932.html

时间: 2024-08-04 07:09:15

线程停止(stop() interrupt() interrupted() isInterrupted())的相关文章

java多线程 interrupt(), interrupted(), isInterrupted()方法区别

interrupt()方法: 作用是中断线程. 本线程中断自身是被允许的,且"中断标记"设置为true 其它线程调用本线程的interrupt()方法时,会通过checkAccess()检查权限.这有可能抛出SecurityException异常.  若线程在阻塞状态时,调用了它的interrupt()方法,那么它的"中断状态"会被清除并且会收到一个InterruptedException异常. 例如,线程通过wait()进入阻塞状态,此时通过interrupt()

interrupt interrupted isInterrupted 区别

1.interrupt interrupt方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位,不会停止线程.需要用户自己去监视线程的状态为并做处理.支持线程中断的方法(也就是线程中断后会抛出interruptedException的方法)就是在监视线程的中断状态,一旦线程的中断状态被置为"中断状态",就会抛出中断异常. 2.interrupted 和 isInterrupted 首先看一下该方法的实现: 1 pu

Thread的Interrupt、isInterrupted、interrupted

线程中断:Interrupt.isInterrupted.interrupted 线程并不是抢占式的,线程是协作式的. Interrupt:声明此线程中断,但是线程并不会立即中断: isInterrupted:判断此线程是否已中断,判断完后不修改线程的中断状态: interrupted:判断此线程是否已中断,判断完后清除线程的中断状态 线程的中断状态默认为( isInterrupted=false 或 interrupted=false ),也就是默认不中断线程 Interrupt理解: 中断线

Thread的中断机制(interrupt),循环线程停止的方法

一.中断原理 中断线程 线程的thread.interrupt()方法是中断线程,将会设置该线程的中断状态位,即设置为true,中断的结果线程是死亡.还是等待新的任务或是继续运行至下一步,就取决于这个程序本身.线程会不时地检测这个中断标示位,以判断线程是否应该被中断(中断标示值是否为true).它并不像stop方法那样会中断一个正在运行的线程. 判断线程是否被中断 判断某个线程是否已被发送过中断请求,请使用Thread.currentThread().isInterrupted()方法(因为它将

多线程-interrupt(),isInterrupted(),interrupted()(转)

Content 背景 中断 相关方法 阻塞方法 不可中断的阻塞方法 处理不支持中断的线程中断的常用方法 处理InterruptedException 待决中断 实例1 实例2 参考资料 Top 背景 由于使用stop方法停止线程非常暴力,可能会导致一系列问题.因此,提出一种温和的方式:请求另外一个先不要在执行了,这就是中断方式. 此外有这样的场景:编写 一个程序,需要暂停一段时间,于是调用Thread.sleep(),但是编译器或IDE报错说没有处理检查到的InterruptedExceptio

Java 线程的终止-interrupt

Java线程的终止--interrupt 取消/关闭的场景 我们知道,通过线程的start方法启动一个线程后,线程开始执行run方法,run方法运行结束后线程退出,那为什么还需要结束一个线程呢?有多种情况,比如说: 很多线程的运行模式是死循环,比如在生产者/消费者模式中,消费者主体就是一个死循环,它不停的从队列中接受任务,执行任务,在停止程序时,我们需要一种"优雅"的方法以关闭该线程. 在一些图形用户界面程序中,线程是用户启动的,完成一些任务,比如从远程服务器上下载一个文件,在下载过程

Java 线程停止、暂停和继续

Thread 类中停止线程的方法有 stop(),暂停和继续线程的方法有 suspend() 和 resume().然而这些方法已经被废弃了. 异常法停止线程 上代码: public class Test { public static void main(String[] args) { MyThread thread = new MyThread(); thread.start(); try { Thread.sleep(1000); } catch (InterruptedExceptio

从头认识java-17.2 线程中断(interrupt)

这一章节我们来讨论一下线程中断(interrupt). 1.什么是线程中断(interrupt)? 就是在多线程运行的时候,我们给线程贴上一个中断的标记,但是不要求线程终止. 2.例子: 中断的例子: package com.ray.ch17; public class Test2 { public static void main(String[] args) { PrintA printA = new PrintA(); Thread threadA = new Thread(printA)

java 线程停止的方法

线程停止的方法: 记住一点,线程的停止, 1.自己运行完了, 2.如果是无限循环,在外部,只能用flag标记来操作. 但是当线程处于冻结状态(阻塞),sleep,wait,join,(这三个方法抛出异常.) 就不会读取到flag. 这个时候,我们要清除线程的冻结状态,让它回到运行中. 如果,线程没有使之冻结的语句,则,inierrupt()不进行任何操作. interrupt()方法,会使sleep,wait,jion方法抛出异常. 然后,我们在处理异常的语句中来操作. 我推荐的方式:不在cat