标签: interrupted-exception

在Thread.join()之前调用Thread.interrupt()会导致join()立即抛出InterruptedException吗?

基本上,问题标题是什么.

Thread t = new Thread(someRunnable);
t.start();
t.interrupt();
t.join(); //does an InterruptedException get thrown immediately here?
Run Code Online (Sandbox Code Playgroud)

从我自己的测试来看,似乎,但只是想确定.我猜测在执行"等待"例程之前Thread.join()检查interrupted线程的状态?

java concurrency multithreading interrupt interrupted-exception

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

Guava:Throwables.propagate和InterruptedException

InterruptedException在Guava中使用Throwables.propagate(e)时,处理s 的最佳实践是什么?

我喜欢使用throw Throwables.propagate(e),特别是在不抛出任何检查异常的方法中,以及异常处理是调用者的责任.但它没有做我对InterruptedException的期望.

我不想失去线程被中断的事实,所以我最终写下这样的事情:

public void run() {
    Callable c = ...;
    try {
        c.call();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw Throwables.propagate(e);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在番石榴做到这一点?是否存在(向后兼容的?!)方式使用类似Throwables.propagate()的方法将线程设置为中断,如果它正在包装并传播InterruptedException?

java exception-handling interrupted-exception guava

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

谁打断了我的线程?

我理解InterruptedException的作用以及它被抛出的原因.但是在我的应用程序中,我在等待SwingUtilities.invokeAndWait()一个只有我的应用程序知道的线程时得到它,而我的应用程序从不调用Thread.interrupt()任何线程,它也永远不会将线程的引用传递给任何人.

所以我的问题是:谁打断了我的线程?

有什么方法可以说出来吗?是否有理由说InterruptedException不包含请求中断的Thread的名称?

我读到它可能是一个框架或库来执行此操作,我们使用以下内容,但我无法想到它们中断我的线程的原因:

  • 过冬
  • 弹簧
  • Log4J的
  • Mysql连接器

java swing multithreading interrupt interrupted-exception

12
推荐指数
2
解决办法
1323
查看次数

调用thread.isInterrupted并打印结果时的不同行为

thread.isInterrupted对下面程序中的行为感到有点困惑.

public class ThreadPractice {

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

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("Starting thread..." + Thread.currentThread().getName());
                    Thread.sleep(10000);
                    System.out.println("Waking up");
                }catch(InterruptedException e){
                    System.out.println("Thread is interrupted!!!");
                    Thread.currentThread().interrupt();
                }
            }
        });

        t.start();
        Thread.sleep(2000);
        t.interrupt();
        //System.out.println(!t.isInterrupted());
        while(!t.isInterrupted()){
            System.out.println("Current thread not interrupted!!!");
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

当执行上述程序时,它打印,

Starting thread...Thread-0
Thread is interrupted!!!
Run Code Online (Sandbox Code Playgroud)

但是当我取消注释System.out语句以打印中断状态时,它会遇到无限循环打印"当前线程未被中断"

我无法弄清楚System.out语句的确切区别.

java multithreading interrupted-exception

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

为什么要捕获InterruptedException来调用Thread.currentThread.interrupt()?

在Effective Java(第275页)中,有以下代码段:

...
for (int i = 0; i < concurrency; i++) {
  executor.execute(new Runnable() {
    public void run() {
    ready.countDown();
    try {
      start.await();
      action.run();
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
    } finally {
      done.countDown();
    }
  }
}
...
Run Code Online (Sandbox Code Playgroud)

捕获被中断的异常只是为了重新提升它有什么用?为什么不让它飞?

java multithreading interrupted-exception

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

如果semaphore.acquire()得到InterruptedException,需要semaphore.relase()吗?

从Java java.util.concurrent.Semaphore文档中我不太清楚,如果semaphore.acquire()阻塞线程并且之后被InterruptedException中断,会发生什么.信号量值是否已降低,是否需要释放信号量?

目前我使用的代码如下:

try {
  // use semaphore to limit number of parallel threads
  semaphore.acquire();
  doMyWork();
}
finally {
  semaphore.release();
}
Run Code Online (Sandbox Code Playgroud)

或者我应该在acquire()期间发生InterruptedException时不要调用release()?

java semaphore interrupted-exception

11
推荐指数
2
解决办法
9486
查看次数

Android MIDI Threading InteruptedException - 触后消息

试图在我的Android应用程序上运行MIDI.我正在按照midisuite示例来配置我的应用程序,除了触后功能,它工作正常.每当我尝试触发触后时,我都会遇到线程异常类型 InteruptedException.我该如何防止这个线程问题?我对多线程的了解并不是最好的,否则我已经想到了这一点.我现在真正能说的是,消息发送得太快,线程还没有从睡眠呼叫中唤醒.

我用我的代码跟着github repo如下:

MidiReceiver 子类:

@TargetApi(Build.VERSION_CODES.M)
public class MidiEngine extends MidiReceiver {

    public  AudioActivity activity;
    private MidiEventScheduler eventScheduler;
    private MidiFramer midiFramer;
    private MidiReceiver midiReceiver = new MyReceiver();

    private Thread mThread;
    private boolean go;
    private int mProgram;

    public MidiEngine() {
        this(new AudioActivity());
    }

    public MidiEngine(AudioActivity activity) {
        this.activity = activity;
        midiReceiver = new MyReceiver();
        midiFramer = new MidiFramer(midiReceiver);
    }

    public AudioActivity getActivity() {
        return this.activity;
    }

    /* This will be called when MIDI data arrives. */
    @Override …
Run Code Online (Sandbox Code Playgroud)

java midi multithreading android interrupted-exception

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

取消文件打开对话框后的InterruptedException - 1.6.0_26

以下代码的输出是:

java.vendor     Sun Microsystems Inc.
java.version    1.6.0_26
java.runtime.version    1.6.0_26-b03
sun.arch.data.model     32
os.name     Windows XP
os.version  5.1
os.arch     x86
Input selection cancelled by user.
Exception while removing reference: java.lang.InterruptedException
java.lang.InterruptedException
    at java.lang.Object.wait(Native Method)
    at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    at sun.java2d.Disposer.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Run Code Online (Sandbox Code Playgroud)

以下代码显示了我的计算机上的异常.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUI extends JPanel implements ActionListener {

    private final String newline = System.getProperty("line.separator");
    JButton openButton;
    JTextArea log;
    JFileChooser fc;

    public GUI() {
        super(new BorderLayout());

        log = new JTextArea(20,40); …
Run Code Online (Sandbox Code Playgroud)

java swing jfilechooser interrupted-exception

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

为什么在finally块中睡眠时线程不会被中断

我一直在寻找MSDN,并且无法找到为什么Thread在finally块中睡眠时无法中断的原因.我试过中止没有成功.

有没有什么方法可以在finally块内睡觉时唤醒线程?

Thread t = new Thread(ProcessSomething) {IsBackground = false};
t.Start();
Thread.Sleep(500);
t.Interrupt();
t.Join();

private static void ProcessSomething()
{
    try { Console.WriteLine("processing"); }
    finally
    {
        try
        {
            Thread.Sleep(Timeout.Infinite);
        }
        catch (ThreadInterruptedException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是MSDN声称线程可以在最终块中止:http://msdn.microsoft.com/en-us/library/aa332364(v = vs.71).aspx "线程可能会在最终阻塞时中止正在运行,在这种情况下,finally块被中止."

编辑 我发现Hans Passant评论是最佳答案,因为这解释了为什么Thread有时可以也不能在finally块中被中断/中止.那就是流程正在关闭的时候.谢谢

c# multithreading interrupted-exception

9
推荐指数
2
解决办法
3147
查看次数

为什么linux内核使用陷阱门来处理divide_error异常?

在内核2.6.11.5中,除零异常处理程序设置为:

set_trap_gate(0,&divide_error);
Run Code Online (Sandbox Code Playgroud)

根据"了解Linux内核",用户模式进程无法访问英特尔陷阱门.但是用户模式进程很可能也会生成一个divide_error.那么为什么Linux以这种方式实现呢?

[编辑]我认为问题仍然是开放的,因为set_trap_gate()将IDT条目的DPL值设置为0,这意味着只有CPL = 0(读取内核)代码才能执行它,因此我不清楚如何从该处理程序调用此处理程序用户模式:

#include<stdio.h>

int main(void)
{
    int a = 0;
    int b = 1;

    b = b/a;

    return b;
}
Run Code Online (Sandbox Code Playgroud)

这是编译的gcc div0.c.输出./a.out是:

浮点异常(核心转储)

因此看起来这不是由0陷阱代码划分处理的.

linux x86 kernel interrupted-exception

9
推荐指数
3
解决办法
3186
查看次数