`

Concurrency

阅读更多
Interrupts
An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate. This is the usage emphasized in this lesson.

A thread sends an interrupt by invoking interrupt on the Thread object for the thread to be interrupted. For the interrupt mechanism to work correctly, the interrupted thread must support its own interruption.

Supporting Interruption

How does a thread support its own interruption? This depends on what it's currently doing. If the thread is frequently invoking methods that throw InterruptedException, it simply returns from the run method after it catches that exception. For example, suppose the central message loop in the SleepMessages example were in the run method of a thread's Runnable object. Then it might be modified as follows to support interrupts:
for (int i = 0; i < importantInfo.length; i++) {
    //Pause for 4 seconds
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
        //We've been interrupted: no more messages.
        return;
    }
    //Print a message
    System.out.println(importantInfo[i]);
}
Many methods that throw InterruptedException, such as sleep, are designed to cancel their current operation and return immediately when an interrupt is received.

What if a thread goes a long time without invoking a method that throws InterruptedException? Then it must periodically invoke Thread.interrupted, which returns true if an interrupt has been received. For example:

for (int i = 0; i < inputs.length; i++) {
    heavyCrunch(inputs[i]);
    if (Thread.interrupted()) {
        //We've been interrupted: no more crunching.
        return;
    }
}
In this simple example, the code simply tests for the interrupt and exits the thread if one has been received. In more complex applications, it might make more sense to throw an InterruptedException:
if (Thread.interrupted()) {
    throw new InterruptedException();
}
This allows interrupt handling code to be centralized in a catch clause.

The Interrupt Status Flag

The interrupt mechanism is implemented using an internal flag known as the interrupt status. Invoking Thread.interrupt sets this flag. When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is cleared. The non-static Thread.isInterrupted, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag.

By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so. However, it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.

分享到:
评论

相关推荐

    C++ Concurrency in Action 2nd.pdf

    《C++ Concurrency in Action》第二版是一本专注于C++编程语言中并发和并行编程的权威指南。本书主要侧重于C++11以及后续版本中引入的多线程功能,旨在帮助读者深入理解并有效运用C++并发特性。由于C++11对并发的...

    Java 9 Concurrency Cookbook.2e

    This book covers the most important and useful mechanisms included in version 9 of the Java concurrency API, so you will be able to use them directly in your applications. The mechanisms are as ...

    C++ Concurrency in Action_C++_action_

    《C++ Concurrency in Action》是一本深入探讨C++并发编程的权威著作,作者是知名软件开发者、C++专家Anthony Williams。这本书旨在帮助C++程序员理解和掌握如何在现代多核处理器环境中有效地使用并发和并行编程技术...

    Python Concurrency with asyncio

    Python Concurrency with asyncio

    Concurrency Control and Recovery in Database Systems

    《Concurrency Control and Recovery in Database Systems》这本书全面涵盖了数据库系统中的并发控制与数据恢复的理论和实践,是该领域内一部不可或缺的经典之作。通过深入学习本书,读者可以掌握数据库管理的关键...

    C++ Concurrency In Action

    《C++ Concurrency In Action》是由Anthony Williams编写的关于C++并发编程的书籍。本书主要探讨了C++中的多线程编程技术,包括线程的管理、线程间的数据共享、并发操作的同步以及C++内存模型和原子类型的操作。 ...

    C++ Concurrency in Action, 2nd Edition(完整目录).pdf.zip

    《C++ Concurrency in Action, 2nd Edition》是一本深度探讨C++并发编程的权威著作,由知名软件工程师Anthony Williams撰写。这本书针对C++11及后续版本的并发特性进行了全面而深入的讲解,旨在帮助读者理解和掌握...

    Java.Threads.and.the.Concurrency.Utilities.1484216997

    This concise book empowers all Java developers to master the complexity of the Java thread APIs and concurrency utilities. This knowledge aids the Java developer in writing correct and complex ...

    C++ Concurrency in Action 完整版

    首先,这本书的标题是“C++ Concurrency in Action”,从这个标题可以推断出,书籍的主题是C++语言的并发编程技术。并发编程是一个广泛的话题,涉及到线程的创建、管理、数据共享、同步、内存模型、设计无锁数据结构...

    Concurrency in Go: Tools and Techniques for Developers

    Concurrency in Go: Tools and Techniques for Developers by Katherine Cox-Buday English | 19 July 2017 | ISBN: 1491941197 | ASIN: B0742NH2SG | 240 Pages | AZW3 | 1.15 MB Concurrency can be notoriously...

    Java Concurrency In Practice.pdf

    《Java Concurrency In Practice》是一本关于Java并发编程的经典著作,由Brian Göetz、Tim Peierls、Joshua Bloch、Joseph Bowbeer、David Holmes和Doug Lea共同编写。本书深入探讨了Java平台上的多线程编程技巧,...

Global site tag (gtag.js) - Google Analytics