在内核2.6.11.5中,除零异常处理程序设置为:
set_trap_gate(0,÷_error);
根据"了解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;
}
这是编译的gcc div0.c.输出./a.out是:
浮点异常(核心转储)
因此看起来这不是由0陷阱代码划分处理的.
让我们假设我有一个消耗另一个线程产生的项的线程.其运行方法如下,inQueue为BlockingQueue
boolean shutdown = false;
while (!shutdown) {
    try {
        WorkItem w = inQueue.take();
        w.consume();
    } catch (InterruptedException e) { 
        shutdown = true;
    }
}
此外,一个不同的线程将通过中断此运行的线程来发出没有更多工作项的信号.如果不需要阻塞来检索下一个工作项,take()会抛出一个中断的异常.即如果生产者发出信号说它已完成填充工作队列,是否可能意外地将一些物品留在inQueue中或错过中断?
以下程序演示了该问题(最新的JVM和诸如此类):
public static void main(String[] args) throws InterruptedException {
    // if this is true, both interrupted and isInterrupted work
    final boolean withPrint = false;
    // decide whether to use isInterrupted or interrupted.
    // if this is true, the program never terminates.
    final boolean useIsInterrupted = true;
    ExecutorService executor = Executors.newSingleThreadExecutor();
    final CountDownLatch latch = new CountDownLatch(1);
    Callable<Void> callable = new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            Random random = new Random();
            while (true) {
                if (withPrint) { …可能重复:
如何立即终止套接字IO操作上的线程阻塞?
我有客户端在线程中运行想要从Java中读取套接字.但是在阅读时,也许我想要杀死这个主题.所以我interrupt,但socket的阅读方法是否抛出InterruptedException?我没找到.
那么,我怎么能很好地要求线程在读取套接字时阻塞?
谢谢
java sockets multithreading network-programming interrupted-exception
有时当我调试我的应用程序时,我在RxCachedThreadScheduler-1中遇到InterruptedException.这是跟踪:
Fatal Exception: java.lang.InterruptedException
       at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:1991)
       at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2025)
       at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1048)
       at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:776)
       at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1035)
我有一个自定义视图,我在其中订阅我的observable,如下所示:
@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    sub = FollowHandler.getInstance().getObservable()
            .filter(new Func1<FollowEvent, Boolean>() {
                @Override
                public Boolean call(FollowEvent followEvent) {
                    if(followEvent == null || followEvent.user == null
                            || user == null)
                        return false;
                    return followEvent.user.id == user.id;
                }
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<FollowEvent>() {
                @Override
                public void onCompleted() {}
                @Override
                public void onError(Throwable e) {}
                @Override
                public void onNext(FollowEvent followEvent) {
                    reactToThisNiceEvent(followEvent);
                }
            });
}
@Override
protected …我在Java中遇到了Future.get()的WEIRD问题.它总是返回一个InterruptedException,但奇怪的是,Exception的原因是null,所以我不能告诉谁打断了我..
它变得更糟,因为我在调用get()之前检查,而Future必须完成的工作已经完成.
以下是负责输出的代码.f是Future,而callable返回一个HashMap,其中Agent与其真正无关.对不起,如果有太多的印刷品,我只是想尽可能地提供信息.来自callable的调用方法现在很简单System.out.println("Hola soy agente"),正如您将看到的那样,打印出来,这意味着可调用函数不会导致异常
这是代码:
try
    {
        System.out.println(f.isDone());        //true
        System.out.println(f.isCancelled());   //false
        System.out.println(f.toString());      //FutureTask
        newModdedAgents.putAll(f.get());
    }catch(InterruptedException e)
    {
        System.out.println(f.isDone());        //true
        System.out.println(f.isCancelled());   //false
        System.err.println(e);                 //It is an interruptedException
        System.err.println(e.getCause());     //???? null?
        e.printStackTrace();
    }
和输出
 Hola soy agente
 true
 false
 java.util.concurrent.FutureTask@1c4c94e5
 true
 false
 java.lang.InterruptedException
 null
java.lang.InterruptedException
at     java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1302)
at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:248)
at java.util.concurrent.FutureTask.get(FutureTask.java:111)
at com.pf.simulator.Simulation.simulateStep(Simulation.java:217)
at com.pf.gui.ButtonPanel.doWork(ButtonPanel.java:141)
at com.pf.gui.ButtonPanel$1$1.construct(ButtonPanel.java:198)
at com.pf.gui.SwingWorker$2.run(SwingWorker.java:117)
at java.lang.Thread.run(Thread.java:636)
如果你想看到我将可调用值汇总到线程池的位置...那么这将是它的代码
    for(Callable<HashMap<Integer, Agent>> c : agentCallables)
    {
        Future<HashMap<Integer,Agent>> future = pool.submit(c);
        agentFutureSet.add(future);
    }
然后我迭代这个Set
    for(Future<HashMap<Integer, Agent>> …当我们制作自己的Threads时,这是关于旧Java的问题.有些方法比如在被另一个线程中断时Thread.sleep(100)抛出一个InterruptedException.据我所知,中断意味着另一个线索在说:让我接手吧.
当发生这种情况时,为什么Java要我们处理InterruptedException?
程序员甚至不需要关心线程是否相互中断.他应该能够在线程之间划分工作,并在完成后进行通知.那么Java希望我们处理的原因是什么InteruptedException?它至少应该是RuntimeException
我很困惑,无法理解为什么不应该吞下InterruptedException.
IBM的文章说
当阻塞方法检测到中断并抛出InterruptedException时,它会清除中断状态.如果捕获InterruptedException但无法重新抛出它,则应保留中断发生的证据,以便调用堆栈上方的代码可以了解中断并在需要时响应它
public class TaskRunner implements Runnable {
    private BlockingQueue<Task> queue;
    public TaskRunner(BlockingQueue<Task> queue) { 
        this.queue = queue; 
    }
    public void run() { 
        try {
             while (true) {
                 Task task = queue.take(10, TimeUnit.SECONDS);
                 task.execute();
             }
         }
         catch (InterruptedException e) { 
           Thread.currentThread().interrupt();//preserve the message
             return;//Stop doing whatever I am doing and terminate
         }
    }
}
此外,Java Concurrency in Practice在第7.1.3章:响应中断中更详细地讨论了这一点.它的规则是:
只有实现线程中断策略的代码才可以吞下中断请求.通用任务和库代码永远不应吞下中断请求.
任何人都可以解释高级调用堆栈中的代码如何利用Thread.currentThread()设置的状态.insert(); 在线程终止时在catch块中?
还请解释上面的规则?
虽然我知道 Re-EntrantLocks 和 之间的理论差异synchronized,但我对以下几点感到困惑。
从一篇关于 Javarevisited比较synchronized和Lock对象的文章中看到这个声明:
Java 中 ReentrantLock 和 synchronized 关键字之间还有一个值得注意的区别是,在等待 Lock 时可以中断 Thread 的能力。在synchronized关键字的情况下,线程可能会被阻塞等待锁定,无限期并且无法控制。ReentrantLock 提供了一个叫做 lockInterruptably() 的方法,可以用来在线程等待锁的时候中断它。类似地,如果锁定在特定时间段内不可用,则可以使用带超时的 tryLock() 超时。
根据上述声明,我确实尝试在同步方法(即阻塞等待)上中断线程等待(),但它确实抛出了 InterruptedException。但这种行为与上述声明中的内容相矛盾。
// this method is called from inside run() method of every thread. 
public synchronized int getCount() {
        count++;
        try {
            Thread.sleep(3000);
            System.out.println(Thread.currentThread().getName() + " gets " + count);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return count;
}
....
....
t1.start();
t2.start();
t3.start();
t4.start();
t2.interrupt();
这是我得到的输出:
Thread 1 gets 1 …在Java中,所有标准的阻塞方法都可以通过调用来中断Thread.interrupt(),但是如果我们有Java绑定包装一个自己进行I/O的本机库呢?那么本机代码应该如何挂钩到线程并响应调用Thread.interrupt()?
java java-native-interface multithreading interrupted-exception