当前位置: 首页 > news >正文

网站首页新闻模板/深圳seo推广外包

网站首页新闻模板,深圳seo推广外包,济南集团网站建设公司,怎么做企业网站一级悬浮菜单1.概述 线程中断是一种机制,用于向线程发出信号,告知它必须在方便时停止执行。但是,是否检查中断状态并停止则取决于正在运行的任务。 2.使用Thread.interrupt中断 首先,我们将看到如何中断线程。 当我们在Thread实例上调用 inter…

1.概述
线程中断是一种机制,用于向线程发出信号,告知它必须在方便时停止执行。但是,是否检查中断状态并停止则取决于正在运行的任务。
2.使用Thread.interrupt中断
首先,我们将看到如何中断线程。 当我们在Thread实例上调用 interrupt时,它将设置线程的中断状态,以便代码的其他部分可以检查并对其执行操作。


public class TestInterrupt {public static void main(String[] args) {TestInterrupt testInterrupt=new TestInterrupt();testInterrupt.interruptThread();}public void interruptThread(){final Runnable task = () -> {int i = 0;while (i < Integer.MAX_VALUE) {i++;}};final Thread thread = new Thread(task);thread.start();System.out.println("Is thread interrupted: " + thread.isInterrupted());thread.interrupt();System.out.println("Is thread interrupted: " + thread.isInterrupted());}
}

在此示例中,我们首先创建一个Runnable任务。然后,我们 使用此任务启动一个线程。最后,我们在中断线程之前和之后观察中断状态。
运行样本打印:

Is thread interrupted: false
Is thread interrupted: true

如预期的那样,线程的中断标志在中断调用后变为true。
通常,有两个方面对中断感兴趣:执行任务的代码和拥有线程的代码。此外,它们可以根据中断请求单独执行操作。前面的示例包含任务和线程所有者。我们的代码是所有者,因为它创建了线程。处理中断是所有者的责任。例如,它可以终止线程或执行任何其他操作。由于我们不知道确切的影响,因此 只有所有者必须中断线程。

例如,在使用线程池时,任务不拥有工作线程,它们只是从基础池实现中借用它们。因此,当我们向线程池提交任务时,任务可以响应中断并可能取消其操作。但是它一定不能中断调用Thread.interrupt的工作线程。所述 的ExecutorService实现封装中断机制,并提供执行shutdownNow方法。这种方法使我们能够在线程池的监视下中断工作线程。

3.检查中断状态
在前面的示例中,我们使用Thread.isInterrupted 检查线程是否被中断。 当呼叫者执行此检查时,不会清除中断标志。换句话说,当多次调用时, isInterrupted返回相同的布尔值:

new Thread(() -> {Thread.currentThread().interrupt();System.out.println("Is thread interrupted: " + Thread.currentThread().isInterrupted());System.out.println("Is thread interrupted: " + Thread.currentThread().isInterrupted());
}).start();

在这里,我们首先中断当前线程。然后连续的isInterrupted调用都返回true:

Is thread interrupted: true
Is thread interrupted: true

该线程类还提供了一个静态的变体: Thread.interrupted。它检查当前线程的中断状态。 另外,它清除中断标志:

new Thread(() -> {Thread.currentThread().interrupt();System.out.println("Is thread interrupted: " + Thread.interrupted());System.out.println("Is thread interrupted: " + Thread.interrupted());
}).start();

输出显示此行为:

Is thread interrupted: true
Is thread interrupted: false

4.响应中断
接下来,我们将研究如何响应线程中断。如前所述,一个中断可以有多个接收者:任务和线程。

对于这些任务,我们必须使用它来取消正在运行的操作并在可能的情况下立即退出。这也是大多数Java类库采用的方法。

对于线程,如果代码不拥有线程(例如,在使用线程池时)或不负责应用中断策略,则必须保留中断请求。为此,我们必须抛出 InterruptedException或再次设置状态。

4.1。清除并抛出InterruptedException
作为第一种选择,我们将考虑在interrupt 上引发InterruptedException 。如果任务支持取消,则必须停止执行。取消之后,我们抛出一个 InterruptedException,以使堆栈上较高的代码处理该中断。

看一下示例:

public void handleAndThrowException() throws InterruptedException {final ExecutorService executorService = Executors.newSingleThreadExecutor();final Callable<Void> task = () -> {while (true) {if (Thread.interrupted()) {System.out.println("Interrupted, cleaning up and then throwing exception.");throw new InterruptedException();}// Do work.}};final Future<?> future = executorService.submit(task);TimeUnit.SECONDS.sleep(1); // Wait for some timefinal boolean cancel = future.cancel(true);System.out.println("Is cancelled?: " + cancel);executorService.shutdown();
}

在这里,我们使用线程池来执行任务。在Callable任务中,我们有一个while循环执行一些工作。请注意,我们的任务依赖于线程中断来取消。当它检测到中断时,它将清除并引发 InterruptedException。这样,我们就会有礼貌地表现并保持中断状态,以便池有机会对其执行操作。注意,我们不是直接调用 Thread.interrupt 。相反,我们正在请求一个调用Future.cancel的中断。

4.2。再次清除并中断
在Runnable任务中,我们不能抛出InterruptedException。否则我们可能不想抛出一个检查异常。在这些情况下, 如果清除,我们必须再次设置中断状态。

while (true) {if (Thread.interrupted()) {System.out.println("Interrupted, cleaning up and then throwing exception.");Thread.currentThread().interrupt();return "Canceled";}// Do work.
}

与前面的示例相似,该任务对中断做出响应,并尽快停止。而且,它保持呼叫者代码的中断状态。

4.3。响应InterruptedException
如果我们的代码是调用者并捕获了InterruptedException,则我们具有与前面相同的选项。我们可以重新抛出异常,也可以设置调用中断方法的中断状态 :

try {TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {System.out.println("Interrupted, cleaning up and then setting the interrupt status.");Thread.currentThread().interrupt();return "Canceled";
}return "Ok";

在这里,我们选择第二种方法并再次设置状态。

4.4。检查不清除
或者,我们可以使用 isInterrupted 检查状态。由于它不会清除状态,因此我们不需要抛出 InterruptedException或再次设置状态。

while (true) {if (Thread.currentThread().isInterrupted()) {System.out.println("Interrupted, cleaning up and then throwing exception.");return "Canceled";}// Do work.
}

4.5吞下中断请求
最后,如果代码对线程应用了中断策略,则可以清除中断状态。这意味着代码不在线程池上运行,而是管理自己的线程。

http://www.lbrq.cn/news/1279981.html

相关文章:

  • 饲料网站源码/深圳今天重大事件新闻
  • 做网站开发app/济南网站seo
  • 2003系统做网站/百度怎么注册自己的网站
  • 北航做网站公司/进入百度首页官网
  • 赤峰网站开发公司/seo的优点
  • 网站建设公司的公司哪家好/财经新闻最新消息
  • 分析网站的关键词/今日新闻
  • 网站建设公司怎么做的/泉州百度关键词优化
  • 最专业的外贸网站建设/新站快速收录
  • 云服务器建网站/阿里云域名查询
  • 代做道路毕业设计网站/关键词seo培训
  • 做电子请帖网站有哪些/seo百度首页排名业务
  • 好推建站/pc网站优化排名
  • 做网站总结体会/优化方案英语
  • wordpress首页文件/seo人员招聘
  • html企业网站源码下载/百度公司地址
  • 国内网站开发不用wordpress/电子商务网站建设流程
  • 网站每个月8g流量/今日刚刚发生的军事新闻
  • 长春做网站 长春万网/自己怎么免费做百度推广
  • 广东网站建设服务供应商/做百度推广一个月多少钱
  • 东营网站建设方案/有哪些平台可以发布推广信息
  • 网站模版超市/拓客最有效方案
  • 做暧暧网站在线/关键词推广排名
  • 域名备案未做网站/seo外链推广
  • 政府网站维护运行方案/百度下载app下载安装
  • 山东省住房建设厅网站考试项目/深圳市seo上词贵不贵
  • 安徽建站贵吗/苏州seo
  • 自己做网站投入/seo怎么提升关键词的排名
  • 网站做qq登录界面/买淘宝店铺多少钱一个
  • 网站建设广告图/最全的百度网盘搜索引擎
  • k8s pod生命周期、初始化容器、钩子函数、容器探测、重启策略
  • 集合框架学习
  • 孤儿进程、僵尸进程和守护进程
  • 工业控制系统安全之 Modbus 协议中间人攻击(MITM)分析与防范
  • 零基础学习性能测试第三章:jmeter构建性能业务场景
  • python 检测蜂窝网络,实现掉网自动拨号