7. java 终止线程

     Java 提供了一种协作的机制,这样 一个线程就能 终止另一个线程 正在做的事。
        7.1. Task Cancellation
         用户请求 或者 时间到了 都会 产生 终止任务的需求
        7.1.1. Interruption
         使用interruption 来取消任务
        7.1.2. Interruption Policies
         中断策略 是 线程在 接收到 中断请求 所做出的应对
        7.1.3. Responding to Interruption
         像 Thread.sleep 或者 blockingQueue.put 都会 响应 中断,抛出 InterruptionException。
        7.1.4. Example: Timed Run
           不能 把中断 放到一个 借来的 线程里。
        7.1.5. Cancellation Via Future
           future.cancel() 取消任务。
        7.1.6. Dealing with Non-interruptible Blocking
           inputStream outputStream 在调用 read write时, 是不可中断的。可以调用close实现中断。
        7.1.7. Encapsulating nonstandard cancellation with newtaskFor
 
  7.2. Stopping a Thread-based Service
        ExecutorService 提供了 shutdown 和 shutdownNow
        7.2.1. Example: A Logging Service
           任务里,没有 响应中断,没有处理 队列里剩下的 为输出的日志
        7.2.2. ExecutorService Shutdown
           用 executorService 的 shutdown,可以中断 blockQueue的put
        7.2.3. Poison Pills
           生产者消费者模式的 结束方式
        7.2.4. Example: A One-shot Execution Service
           把ThreadPool 放到局部变量
  	     7.2.5. Limitations of Shutdownnow
  	        不能返回 执行一半的任务。
 7.3. Handling Abnormal Thread Termination
           有些线程意外 会终止
        7.3.1. Uncaught Exception Handlers
           实现UncaughtExceptionHandler 接口, 并把它 注册进 JVM
  7.4. JVM Shutdown
       JVM 关闭 分 有序 和 突然
        7.4.1. Shutdown Hooks
           Runtime.addShutdownHook 添加勾
        7.4.2. Daemon Threads
           JVM 关闭了依然可以运行
        7.4.3. Finalizers
        Summary
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值