java最后一个线程汇总数据,JAVA 线程基本知识汇总--线程中断

1.线程中断的结果专业术语

isInterrupted interrupted interrupt

// 测试当前线程是否已经中断,同时会将线程的中断状态取消

Thread.interrupted();

// 在当前线程加上一个打断标记 ** 并不会真的立即停止线程

thread.interrupt();

// 测试线程是否已经中断

thread.isInterrupted();

2.知识点:

直接调用interrupt 不会中断线程

package org.test;

/*

* 线程中断演示

*

* @author patzheng 597439394@163.com

*

*/

public class Yak {

public static void main(String[] args) {

Thread countThread = new Thread(new CountRunnable(10));

countThread.start();

countThread.interrupt();

System.err.println("子线程被主线程给终止");

}

}

class CountRunnable implements Runnable {

private int maxValue;

public CountRunnable(int maxValue) {

this.maxValue = maxValue;

}

@Override

public void run() {

int sum = 0;

for (int i = 0; i 

sum += i;

Long now = System.currentTimeMillis();

while (System.currentTimeMillis() - now 

}

}

System.err.println(sum);

}

}

换个方式终止线程:使用break 语句结束循环,但是循环之外的语句还是会执行。使用的是

countThread.interrupt();可以看到终止之后线程的isInterupted返回的都是true;如果换成interupted的话就会输出

false 意味着线程的终止状态被清除

package org.test;

import java.text.BreakIterator;

/*

* 线程中断演示

*

* @author patzheng 597439394@163.com

*

*/

public class Yak {

public static void main(String[] args) {

Thread countThread = new Thread(new CountRunnable(10));

countThread.start();

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

e.printStackTrace();

}

countThread.interrupt();

System.err.println("子线程被主线程给终止");

}

}

/**

* 再runnable 里面直接用Thread.currentThread 就可以使用Thread的一些方法了

*

* @author patzheng 597439394@163.com

*

*/

class CountRunnable implements Runnable {

private int maxValue;

public CountRunnable(int maxValue) {

this.maxValue = maxValue;

}

@Override

public void run() {

int sum = 0;

for (int i = 0; i 

if (!Thread.currentThread().isInterrupted()) {

sum += i;

} else {

System.err.println(Thread.currentThread().isInterrupted()

+ "status");

System.err.println(Thread.currentThread().isInterrupted()

+ "status");

System.err.println("break");

break;

}

Long now = System.currentTimeMillis();

while (System.currentTimeMillis() - now 

}

}

System.err.println(sum);

}

}

接下来通过异常来处理:

package org.test;

/*

* 线程中断演示

*

* @author patzheng 597439394@163.com

*

*/

public class Yak {

public static void main(String[] args) {

Thread countThread = new Thread(new CountRunnable(10));

countThread.start();

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

e.printStackTrace();

}

countThread.interrupt();

System.err.println("子线程被主线程给终止");

}

}

/**

* 再runnable 里面直接用Thread.currentThread 就可以使用Thread的一些方法了

*

* @author patzheng 597439394@163.com

*

*/

class CountRunnable implements Runnable {

private int maxValue;

public CountRunnable(int maxValue) {

this.maxValue = maxValue;

}

@Override

public void run() {

try {

int sum = 0;

for (int i = 0; i 

if (!Thread.interrupted()) {

sum += i;

} else {

throw new InterruptedException(" thread interrupted");

}

Long now = System.currentTimeMillis();

while (System.currentTimeMillis() - now 

}

}

System.err.println(sum);

} catch (Exception e) {

e.printStackTrace();

}

}

}

线程在sleep,wait,join 的情况下呗interrupt回发生什么?都直接抛出异常 这也是为什么使用这些方法的时候需要捕获

InterruptedException异常  而且中断状态被清除

并非所有的阻塞方法都抛出InterruptedException。输入和输出流类会阻塞等待 I/O 完成,但是它们不抛出InterruptedException,而且在被中断的情况下也不会提前返回。然而,对于套接字 I/O,如果一个线程关闭套接字,则那个套接字上的阻塞 I/O 操作将提前结束,并抛出一个SocketException。java.nio中的非阻塞 I/O 类也不支持可中断 I/O,但是同样可以通过关闭通道或者请求Selector上的唤醒来取消阻塞操作。类似地,尝试获取一个内部锁的操作(进入一个synchronized块)是不能被中断的,但是ReentrantLock支持可中断的获取模式。

最佳实践 对于这个异常最好的处理办法是在异常处理中将中断复位方便接下来的处理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java多线程数据汇总: 1. 线程的概念和创建方式: 线程是程序执行的基本单元,Java中可以通过继承Thread类或实现Runnable接口来创建线程。 2. 线程同步的方法: Java中常用的线程同步方法包括synchronized关键字、Lock接口、ReentrantLock类、Condition接口等。 3. 线程池的概念和使用: 线程池是一种多线程处理的方式,可以通过ThreadPoolExecutor类来创建线程池,提高多线程处理的效率。 4. 线程安全的集合类: Java中提供了多种线程安全的集合类,如ConcurrentHashMap、ConcurrentLinkedQueue、CopyOnWriteArrayList等,可以在多线程环境下安全地访问集合。 5. 线程通信的方法: Java线程通信的方法包括wait()、notify()、notifyAll()等,可以实现线程之间的协作和通信。 6. 线程的状态: Java线程的状态包括新建、就绪、运行、阻塞和死亡等。 7. 线程的优先级: Java线程的优先级可以通过setPriority()方法设置,优先级高的线程有更大的概率被调度执行。 8. 线程中断Java中可以通过interrupt()方法对线程进行中断线程可以通过isInterrupted()或interrupted()方法判断是否被中断。 9. 线程的异常处理: Java中可以通过try-catch语句对线程的异常进行处理,也可以通过UncaughtExceptionHandler接口来自定义线程的异常处理方式。 10. 线程的停止: Java中可以通过设置标志位或调用stop()方法来停止线程,但是stop()方法不建议使用,因为它可能导致线程不安全的情况发生。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值