Java – thread interrupt: will it cancel receiving the wait () call?

I have a thread that has an incoming job queue (a LinkedList containing job descriptions) When no task can work, the thread waits in the queue () When an external job scheduler object places a new job in the queue, it wakes it up with notify ()

When my program closes, I call interrupt() on thread This raises interruptedexception when a thread waits for a job in wait() My question is: what happens if I interrupt the thread instead of blocking but do some work? The item being processed is the last in the queue (so the queue is now empty), and the isinterrupted () check flag is set before the interrupt. It calls wait () again? It will throw an interruptedexception because the interrupt flag has been set or the thread will always wait, because the new job will never reach the queue, and no one will interrupt the wait?

Solution

Yes, the thread you interrupt will throw an interruptedexception exception when calling wait () This is a very simple test of yourself

public class TestInt {
    public static void main(String[] args) throws Exception
    {
        Thread.currentThread().interrupt();

        synchronized(TestInt.class) {
            TestInt.class.wait();
        }    
    }    
}

Also note the Java ODC of objectodwait():

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>