加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何在java中确保一个代码块不能被任何其他线程中断

发布时间:2020-12-14 05:26:27 所属栏目:Java 来源:网络整理
导读:为例: new Thread(new Runnable() { public void run() { while(condition) { *code that must not be interrupted* *some more code* } }}).start();SomeOtherThread.start();YetAntherThread.start(); 如何确保不会中断的代码不会中断? 解决方法 您不能
为例:
new Thread(new Runnable() {
  public void run() {
    while(condition) {

      *code that must not be interrupted*

      *some more code*
    }
  }
}).start();

SomeOtherThread.start();

YetAntherThread.start();

如何确保不会中断的代码不会中断?

解决方法

您不能 – 至少不能使用正常的Java,在正常的非实时操作系统上运行.即使其他线程不会中断你的,其他进程也可能这样做.基本上,您将无法保证在完成操作之前,您将获得一个CPU.如果你想要这样的保证,你应该使用Java实时系统.我不知道是否知道这肯定会提供你想要的设施.

最好的做法是首先避免这个要求.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读