本文讲述,spark中的不中断线程的内容。
直接看代码:UninterruptibleThread
主要作用是重写 Thread类的interrupt方法,在执行thread.interrupt()方法的时候
增加了一个判断uninterruptible(或者说是一个锁,在线程执行完成之后,通过finally进行释放),
如果这个值为ture,打断不起作用。默认值false。
uninterruptible属性的值默认值为false,如果想更改这个属性的值,需要在执行线程的时候。
调用def runUninterruptibly[T](f: => T): T {} 方法。
由于是多线程,在代码中考虑到了同步问题,使用uninterruptibleLock 加锁。
代码以及测试用例如下:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zl.unused
/**
* A special Thread that provides "runUninterruptibly" to allow running codes without being
* interrupted by `Thread.interrupt()`. If `Thread.interrupt()` is called during runUninterruptibly
* is running, it won't set the interrupted status. Instead, setting the interrupted status will be
* deferred until it's returning from "runUninterruptibly".
*
* Note: "runUninterruptibly" should be called only in `this` thread.
*/
private class UninterruptibleThread(
target: Runnable,
name: String) extends Thread(target, name) {
def this(name: String) {
this(null, name)
}
/** A monitor to protect "uninterruptible" and "interrupted" */
private val uninterruptibleLock = new Object
/**
* Indicates if `this` thread are in the uninterruptible status. If so, interrupting
* "this" will be deferred until `this` enters into the interruptible status.
*/
private var uninterruptible = false
/**
* Indicates if we should interrupt `this` when we are leaving the uninterruptible zone.
*/
private var shouldInterruptThread = false
/**
* Run `f` uninterruptibly in `this` thread. The thread won't be interrupted before returning
* from `f`.
*
* Note: this method should be called only in `this` thread.
*/
def runUninterruptibly[T](f: => T): T = {
if (Thread.currentThread() != this) {
throw new IllegalStateException(s"Call runUninterruptibly in a wrong thread. " +
s"Expected: $this but was ${Thread.currentThread()}")
}
if (uninterruptibleLock.synchronized { uninterruptible }) {
println("We are already in the uninterruptible status. So just run \"f\" and return")
return f
}
uninterruptibleLock.synchronized {
println("Clear the interrupted status if it's set.")
shouldInterruptThread = Thread.interrupted() || shouldInterruptThread
uninterruptible = true
}
try {
f
} finally {
println(" rest information ..... ")
uninterruptibleLock.synchronized {
uninterruptible = false
if (shouldInterruptThread) {
// Recover the interrupted status
super.interrupt()
shouldInterruptThread = false
}
}
}
}
/**
* Interrupt `this` thread if possible. If `this` is in the uninterruptible status, it won't be
* interrupted until it enters into the interruptible status.
*/
override def interrupt(): Unit = {
uninterruptibleLock.synchronized {
if (uninterruptible) {
println("该线程是无法中断线程,需要等程序运行完,自行中断.........")
shouldInterruptThread = true
} else {
println("该线程是可以中断线程,正在执行中断操作.........")
super.interrupt()
}
}
}
}
运行样例:
def main(args: Array[String]): Unit = {
val enterRunUninterruptibly = new CountDownLatch(1)
val interruptLatch = new CountDownLatch(1)
@volatile var hasInterruptedException = false
@volatile var interruptStatusBeforeExit = false
@volatile var index = 0 ;
val t = new UninterruptibleThread("test") {
override def run(): Unit = {
runUninterruptibly {
while (true){
// 这里是一个死循环,如果一直运行,则程序永远无法中断
}
}
interruptStatusBeforeExit = Thread.interrupted()
}
}
t.start()
//不断尝试进行中断,如果线程中断,程序返回结果。
while (true){
t.interrupt()
if(true == Thread.interrupted()){
return
}
}
interruptLatch.countDown()
println("hasInterruptedException : " + hasInterruptedException)
println("interruptStatusBeforeExit : " + interruptStatusBeforeExit)
}
输出结果:
其他运行样例:
package com.zl.unused
import java.util.concurrent.{CountDownLatch, TimeUnit}
import com.google.common.util.concurrent.Uninterruptibles
import scala.util.Random
object Test {
def main(args: Array[String]): Unit = {
test04()
}
// test01("interrupt when runUninterruptibly is running") {
def test01(){
val enterRunUninterruptibly = new CountDownLatch(1)
@volatile var hasInterruptedException = false
@volatile var interruptStatusBeforeExit = false
val t = new UninterruptibleThread("test") {
override def run(): Unit = {
runUninterruptibly {
enterRunUninterruptibly.countDown()
hasInterruptedException = sleep(1000)
}
interruptStatusBeforeExit = Thread.interrupted()
}
}
t.start()
assert(enterRunUninterruptibly.await(10, TimeUnit.SECONDS), "await timeout")
t.interrupt()
t.join()
assert(hasInterruptedException == false)
assert(interruptStatusBeforeExit == true)
}
def test02(){
val interruptLatch = new CountDownLatch(1)
@volatile var hasInterruptedException = false
@volatile var interruptStatusBeforeExit = false
val t = new UninterruptibleThread("test") {
override def run(): Unit = {
Uninterruptibles.awaitUninterruptibly(interruptLatch, 10, TimeUnit.SECONDS)
try {
runUninterruptibly {
println(" interrupt before runUninterruptibly runs ")
}
} catch {
case _: InterruptedException => hasInterruptedException = true
}
interruptStatusBeforeExit = Thread.interrupted()
}
}
t.start()
t.interrupt()
interruptLatch.countDown()
t.join()
assert(hasInterruptedException == false)
assert(interruptStatusBeforeExit == true)
}
// test("nested runUninterruptibly") {
def test03(){
val enterRunUninterruptibly = new CountDownLatch(1)
val interruptLatch = new CountDownLatch(1)
@volatile var hasInterruptedException = false
@volatile var interruptStatusBeforeExit = false
val t = new UninterruptibleThread("test") {
override def run(): Unit = {
runUninterruptibly {
enterRunUninterruptibly.countDown()
Uninterruptibles.awaitUninterruptibly(interruptLatch, 10, TimeUnit.SECONDS)
hasInterruptedException = sleep(1)
runUninterruptibly {
if (sleep(1)) {
hasInterruptedException = true
}
}
if (sleep(1)) {
hasInterruptedException = true
}
}
interruptStatusBeforeExit = Thread.interrupted()
}
}
t.start()
assert(enterRunUninterruptibly.await(10, TimeUnit.SECONDS), "await timeout")
t.interrupt()
interruptLatch.countDown()
t.join()
assert(hasInterruptedException == false)
assert(interruptStatusBeforeExit == true)
}
def test04(){
val enterRunUninterruptibly = new CountDownLatch(1)
val interruptLatch = new CountDownLatch(1)
@volatile var hasInterruptedException = false
@volatile var interruptStatusBeforeExit = false
@volatile var index = 0 ;
val t = new UninterruptibleThread("test") {
override def run(): Unit = {
runUninterruptibly {
while (true){
// println("Thread.interrupted() : " + Thread.interrupted())
// index = index + 1 ;
}
}
interruptStatusBeforeExit = Thread.interrupted()
}
}
t.start()
// TimeUnit.SECONDS.sleep(10);
while (true){
t.interrupt()
if(true == Thread.interrupted()){
return
}
}
interruptLatch.countDown()
// t.join()
println(hasInterruptedException == false)
println(interruptStatusBeforeExit == true)
}
def test(){
@volatile var hasInterruptedException = false
val t = new UninterruptibleThread("stress test") {
override def run(): Unit = {
for (i <- 0 until 100) {
try {
runUninterruptibly {
if (sleep(Random.nextInt(10))) {
hasInterruptedException = true
}
runUninterruptibly {
if (sleep(Random.nextInt(10))) {
hasInterruptedException = true
}
}
if (sleep(Random.nextInt(10))) {
hasInterruptedException = true
}
}
Uninterruptibles.sleepUninterruptibly(Random.nextInt(10), TimeUnit.MILLISECONDS)
// 50% chance to clear the interrupted status
if (Random.nextBoolean()) {
Thread.interrupted()
}
} catch {
case _: InterruptedException =>
// The first runUninterruptibly may throw InterruptedException if the interrupt status
// is set before running `f`.
}
}
}
}
t.start()
for (i <- 0 until 400) {
Thread.sleep(Random.nextInt(10))
t.interrupt()
}
t.join()
assert(hasInterruptedException == false)
}
/** Sleep millis and return true if it's interrupted */
private def sleep(millis: Long): Boolean = {
try {
Thread.sleep(millis)
false
} catch {
case _: InterruptedException =>
true
}
}
}