标签: interrupted-exception

在ThreadInterrupted的情况下提取进程的退出代码

我刚刚通过exec()调用创建了一个进程,现在我正在使用它的.waitFor()方法.我需要捕获一个InterruptedException,但我不确定我应该在catch代码块中放置什么.我想收到退出代码,但如果当前线程被中断,我不会.如果线程被中断,我该怎么办才能让退出代码退出进程?

例:

import java.io.IOException;


public class Exectest {
public static void main(String args[]){
      int exitval;

      try {
        Process p = Runtime.getRuntime().exec("ls -la ~/");
        exitval = p.waitFor();
        System.out.println(exitval);
    } catch (IOException e) {
        //Call failed, notify user.
    } catch (InterruptedException e) {
        //waitFor() didn't complete. I still want to get the exit val. 
        e.printStackTrace();
    }

}
}
Run Code Online (Sandbox Code Playgroud)

java multithreading process interrupted-exception

4
推荐指数
1
解决办法
9117
查看次数

正在运行时中断汇编指令

当中断到达 CPU 时,如果它被确认,则通过在跳转到处理程序之前保存当前地址位置来处理它。否则将被忽略。

我想知道汇编指令调用是否被中断。

例如,

mvi a, 03h ; put 3 value into acc. in 8080 assembly
Run Code Online (Sandbox Code Playgroud)

一行指令可以中断吗?或者如果不是,它是原子的?

是否总能保证“一行汇编指令”总是原子的?

如果8080汇编中没有“lock”关键字呢,那么原子性是如何提供的呢?

例如,如果要操作64位和,但是“一行​​指令”没有办法做到,并且在对和进行操作时出现中断怎么办?如何在装配级别防止它?

对我来说,这个概念正在开始归结。

assembly interrupt cpu-architecture atomicity interrupted-exception

4
推荐指数
2
解决办法
1081
查看次数

epoll_wait因EINTR而失败,如何解决这个问题?

由于EINTR,我的epoll_wait失败了.我的gdb跟踪显示了这个:

enter code here
221     in ../nptl/sysdeps/pthread/createthread.c
(gdb) 
224     in ../nptl/sysdeps/pthread/createthread.c
(gdb) 
 [New Thread 0x40988490 (LWP 3589)]

227     in ../nptl/sysdeps/pthread/createthread.c
(gdb) 
epoll_wait error in start timer: Measurement will befor entire duration of execution
epoll_wait: Interrupted system call
[Thread 0x40988490 (LWP 3589) exited]
Run Code Online (Sandbox Code Playgroud)

这个字符串"启动计时器中的epoll_wait错误:测量将在整个执行期间"由我在stderr中打印.

我无法弄清楚,如何补救这个EINTR,以便epoll_wait可以工作.知道GDB跟踪如何生成这个EINTR吗?

c linux epoll system-calls interrupted-exception

3
推荐指数
1
解决办法
7556
查看次数

捕获InterruptedException时JVM是否需要退出

正如标题所暗示的,当得到InterruptedException时,我们是否需要强制jvm退出?我问的原因是我有点与(Unix)中断信号混淆.当我们在运行java应用程序时键入Ctrl-C时,阻塞线程是否会出现InterruptedException?

所以简单来说,
1)何时以及为什么InterruptedException被抛出?
2)当JVM从外部中断或终止时(如Unix命令),线程是否会中断?

谢谢

java jvm signals interrupted-exception

3
推荐指数
1
解决办法
676
查看次数

boost :: this_thread :: interruption_point()不会抛出boost :: thread_interrupted&exception

我想使用boost :: thread interrupt()中断一个线程.我有以下代码,不会抛出boost :: thread_interrupted&exception:

int myClass::myFunction (arg1, arg2) try{
//some code here
    do {   
        boost::this_thread::interruption_point(); 
        //some other code here
    } while (counter != 20000); 
}catch (boost::thread_interrupted&) {
    cout << "interrupted" << endl;
}
Run Code Online (Sandbox Code Playgroud)

如果我用boost :: this_thread :: sleep(boost :: posix_time :: milliseconds(150))替换boost :: this_thread :: interruption_point(),则抛出异常并且中断按预期工作.

有人可以解释为什么boost :: this_thread :: interruption_point()不会抛出预期的异常?

c++ boost interrupted-exception

2
推荐指数
1
解决办法
4660
查看次数

为什么我必须在try/catch语句中包装每个Thread.sleep()调用?

我正在尝试用Java编写我的第一个多线程程序.我无法理解为什么我们需要围绕for循环进行此异常处理.当我在没有try/catch子句的情况下编译时,它会产生InterruptedException.(这是消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type InterruptedException
Run Code Online (Sandbox Code Playgroud)

)

但是当使用try/catch运行时,catch块中的sysout永远不会显示 - 这意味着没有捕获到这样的异常!

public class SecondThread implements Runnable{
    Thread t;

    SecondThread(){
        t = new Thread(this, "Thread 2");
        t.start();
    }

    public void run(){
        try{
            for(int i=5 ; i>0 ; i--){
                System.out.println("thread 2: " + i);
                Thread.sleep(1000);
            }
        }
        catch(InterruptedException e){
            System.out.println("thread 2 interrupted");
        }
    }
}


public class MainThread{

    public static void main(String[] args){
        new SecondThread();

        try{
            for(int i=5 ; i>0 ; i--){
                System.out.println("main thread: …
Run Code Online (Sandbox Code Playgroud)

java interrupted-exception interruption

2
推荐指数
1
解决办法
9946
查看次数

软件和硬件中断之间的区别

我最近开始研究ARM Cortex微控制器.在通过Internet阅读不同的文章时,我通​​常会发现2个常见术语,如软件中断和硬件中断.这两者的实际差异是什么?你能用一个例子解释一下吗?

c embedded interrupt-handling embedded-linux interrupted-exception

2
推荐指数
1
解决办法
8649
查看次数

使用 Mockito Spy 对 ThreadPoolTask​​Executor 进行单元测试 InterruptedException

我正在使用 springThreadPoolTaskExecutor在多个线程中执行任务。该类如下所示

    @Component
    public class LoadData {
    //... ...    

        @Inject
        private ThreadPoolTaskExecutor taskExecutor;

        public SomeData getData(Long id) {
            Future<SomeData> loadData = taskExecutor.submit(() -> {
                    //return methodToGetDataSynchronously(id);
                    return new SomeData();
            });
            try {
                SomeData data = loadData.get();
            } catch (InterruptedException | ExecutionException e) {
                logger.error("error");
                //some more processing for the error here
            }
            return data;
       }
  }
Run Code Online (Sandbox Code Playgroud)

为了能够对此类进行单元测试并覆盖InterruptedExceptionExecutionException分支,我尝试了多种方法(使用Mockito Spy),但未能成功测试这一点。

单元测试类如下所示:

@RunWith(MockitoJUnitRunner.class)
public class LoadDataTest {

    @InjectMocks
    private LoadData loadData;

    @Spy
    private ThreadPoolTaskExecutor spyTaskExecutor …
Run Code Online (Sandbox Code Playgroud)

java junit spring mockito interrupted-exception

2
推荐指数
1
解决办法
8900
查看次数

如何在 RxJava 中处理 dispose 而不会出现 InterruptedException

在下面的代码中,当dispose()被调用时,发射器线程被中断(InterruptedException被抛出 sleep 方法)。

    Observable<Integer> obs = Observable.create(emitter -> {
        for (int i = 0; i < 10; i++) {
            if (emitter.isDisposed()) {
                System.out.println("> exiting.");
                emitter.onComplete();
                return;
            }

            emitter.onNext(i);
            System.out.println("> calculation = " + i);


            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        emitter.onComplete();
    });

    Disposable disposable = obs
            .subscribeOn(Schedulers.computation())
            .subscribe(System.out::println);

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    disposable.dispose();
Run Code Online (Sandbox Code Playgroud)

从调试会话中我看到中断源FutureTask在处理期间被取消。在那里,将对照运行线程检查dispose()正在调用的线程,如果不匹配,则发射器被中断。由于我使用了计算,所以线程有所不同Scheduler

有什么方法可以使处理不中断此类发射器,或者实际上应该如何处理?我在这种方法中看到的一个问题是,当我有一个可中断操作(此处通过 sleep 模拟)时,我希望在调用 之前正常完成该操作onComplete() …

java interrupted-exception rx-java rx-java2

2
推荐指数
1
解决办法
3202
查看次数

中断java中正常运行的线程

我试图中断正常运行的线程(未处于 sleep() 或 wait() 状态)。

在网络中浏览时,我知道中断正常运行的线程只会将标志设置为 true 并继续该过程。

代码片段是

one.java

......
......
actionperformedmethod {

if (actionCmd.equals("cancel")) {
    try {
        r1.stop();  // to two.java
    } catch (InterruptedException ex) {
        ....
        ....
    }
}
}
Run Code Online (Sandbox Code Playgroud)

two.java

.....
.....
stop method() throws InterruptedException{
        if(!(t.isInterrupted())){
            t.interrupt();
            throw new InterruptedException();
        }
}
Run Code Online (Sandbox Code Playgroud)

从 Two.java 当我抛出 InterruptedException 时,我可以在 one.java 处获取异常块,但是之后如何停止线程,因为即使在该线程似乎继续正常过程之后。

我对线程概念不熟悉,请帮忙..

java multithreading interrupted-exception

1
推荐指数
1
解决办法
6797
查看次数