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
.
分享到:
相关推荐
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 ...
Concurrency in .NET teaches you how to build concurrent and scalable programs in .NET using the functional paradigm. This intermediate-level guide is aimed at developers, architects, and passionate ...
C++ Concurrency in Action 英文第二版 经典的C++并行并发编程教程
Python Concurrency with asyncio
I had worked with concurrency in several languages, but I had never worked in a language that made concurrency so easy (which is not to say they don’t exist; I just hadn’t used any). I had found my...
HIGHLIGHT C++ Concurrency in Action is the first book to market to show how to take advantage of the new C++ Standard and how to write robust multi-threaded applications in C++. DESCRIPTION With the ...
Master the principles and techniques of multithreaded programming with the Java 8 Concurrency API, About This Book, Implement concurrent applications using the Java 8 Concurrency API and its new ...
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 ...
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...
Starting with the traditional approach to concurrency, you will learn how to write multithreaded concurrent programs and compose ways that won’t require locking. You will explore the concepts of ...
C++ Concurrency in Action C++ Concurrency in Action C++ Concurrency in Action
Java 9 Concurrency Cookbook - Second Edition by Javier Fernandez Gonzalez English | 25 Apr. 2017 | ASIN: B01KOG6V5M | 594 Pages | AZW3 | 4.11 MB Key Features Get detailed coverage of important ...
Java Concurrency in Practice 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...
Java.Concurrency.in.Practice.pdf
Apply the core principles of concurrency to both browser and server side development Explore the latest tools and techniques at the forefront of concurrent programming, including JavaScript promises, ...
1.1. A (Very) Brief History of Concurrency 2 1.2. Benefits of Threads 3 1.2.1.ExploitingMultipleProcessors 3 1.2.2.SimplicityofModeling 3 1.2.3.SimplifiedHandlingofAsynchronousEvents 3 1.2.4....
This book is an in-depth guide to the concurrency and multithreading facilities from the new C++ Standard, from the basic usage of std::thread, std::mutex, and std:: async, to the complexities of ...
And while this is great, there's a niche that until very recently was largely unserved: powerful, compiled, cross-platform languages with concurrency support that are geared towards systems ...
并行编程>>英文版chm格式,英文名称<Java Concurrency in Practice>,一直想买这本书,但总是缺货,找到了电子版,分享给大家。 Java Concurrency in Practice By Brian Goetz, Tim Peierls, Joshua Bloch, ...