从头认识多线程-2.6 当异常出现时,线程自动释放锁
来源:互联网 发布:彩八仙时时彩软件 编辑:程序博客网 时间:2024/05/17 09:21
这一章节我们来讨论一下下面的情况:当异常出现时,线程自动释放锁。
1.一般情况
package com.ray.deepintothread.ch02.topic_6;import java.util.Random;public class ReleaseTheLockWhenException {public static void main(String[] args) throws InterruptedException {MyTestObjectOne myTestObjectOne = new MyTestObjectOne();ThreadOne threadOne = new ThreadOne(myTestObjectOne);Thread thread = new Thread(threadOne);thread.start();ThreadTwo threadTwo = new ThreadTwo(myTestObjectOne);Thread thread2 = new Thread(threadTwo);thread2.start();}}class ThreadOne implements Runnable {private MyTestObjectOne myTestObjectOne;public ThreadOne(MyTestObjectOne myTestObjectOne) {this.myTestObjectOne = myTestObjectOne;}@Overridepublic void run() {try {myTestObjectOne.service_1();} catch (InterruptedException e) {e.printStackTrace();}}}class ThreadTwo implements Runnable {private MyTestObjectOne myTestObjectOne;public ThreadTwo(MyTestObjectOne myTestObjectOne) {this.myTestObjectOne = myTestObjectOne;}@Overridepublic void run() {try {myTestObjectOne.service_2();} catch (InterruptedException e) {e.printStackTrace();}}}class MyTestObjectOne {public synchronized void service_1() throws InterruptedException {System.out.println("service_1 begin");System.out.println("service_1 working");Thread.sleep(1000);System.out.println("service_1 end");}public synchronized void service_2() throws InterruptedException {System.out.println("service_2 begin");System.out.println("service_2 working");Thread.sleep(1000);System.out.println("service_2 end");}}
输出:
service_1 beginservice_1 workingservice_1 endservice_2 beginservice_2 workingservice_2 end
从输出和之前的文章我们可以知道,上面的情况是哪个线程先得到锁,就先执行,然后执行完了,才到下一个线程得到锁,因此输出结果是一个方法一个方法的输出
2.我们加上人造异常的情况
package com.ray.deepintothread.ch02.topic_6;import java.util.Random;public class ReleaseTheLockWhenException {public static void main(String[] args) throws InterruptedException {MyTestObjectOne myTestObjectOne = new MyTestObjectOne();ThreadOne threadOne = new ThreadOne(myTestObjectOne);Thread thread = new Thread(threadOne);thread.start();ThreadTwo threadTwo = new ThreadTwo(myTestObjectOne);Thread thread2 = new Thread(threadTwo);thread2.start();}}class ThreadOne implements Runnable {private MyTestObjectOne myTestObjectOne;public ThreadOne(MyTestObjectOne myTestObjectOne) {this.myTestObjectOne = myTestObjectOne;}@Overridepublic void run() {try {myTestObjectOne.service_1();} catch (InterruptedException e) {e.printStackTrace();}}}class ThreadTwo implements Runnable {private MyTestObjectOne myTestObjectOne;public ThreadTwo(MyTestObjectOne myTestObjectOne) {this.myTestObjectOne = myTestObjectOne;}@Overridepublic void run() {try {myTestObjectOne.service_2();} catch (InterruptedException e) {e.printStackTrace();}}}class MyTestObjectOne {public synchronized void service_1() throws InterruptedException {System.out.println("service_1 begin");System.out.println("service_1 working");{// 人造异常String a = null;a.toString();}Thread.sleep(1000);System.out.println("service_1 end");}public synchronized void service_2() throws InterruptedException {System.out.println("service_2 begin");System.out.println("service_2 working");Thread.sleep(1000);System.out.println("service_2 end");}}
输出:
service_1 begin
Exception in thread "Thread-0"
service_1 working
service_2 begin
service_2 working
java.lang.NullPointerException
at com.ray.deepintothread.ch02.topic_6.MyTestObjectOne.service_1(ReleaseTheLockWhenException.java:59)
at com.ray.deepintothread.ch02.topic_6.ThreadOne.run(ReleaseTheLockWhenException.java:28)
at java.lang.Thread.run(Thread.java:745)
service_2 end
从上面的输出可以看见,当线程0出现异常的时候,紧接着线程1就开始执行,对比上面的结果,由此可见,当出现异常的时候,线程0已经释放锁,让其他的线程得到锁继续执行下去。
总结:这一章节我们讨论了一下当异常出现时,线程自动释放锁的情况。
这一章节就到这里,谢谢
------------------------------------------------------------------------------------
我的github:https://github.com/raylee2015/DeepIntoThread
目录:http://blog.csdn.net/raylee2007/article/details/51204573
- 从头认识多线程-2.6 当异常出现时,线程自动释放锁
- 当异常出现时
- 当异常出现时
- 当异常出现时
- 从头认识多线程-1.17 守护线程setDaemon()
- 从头认识java-18.2 基本的线程机制(8)多线程的异常捕捉
- 从头认识多线程-1.6 迫使线程停止的方法-异常法
- 2.1.7出现异常的线程自动释放锁
- 从头认识多线程-1.7 迫使线程停止的方法-异常法的扩展(Sleep的Exception)
- 从头认识多线程-2.5 锁的可重入性
- 从头认识多线程-目录
- 从头认识多线程-目录
- 从头认识多线程-1.2 共享变量与线程安全简介
- 从头认识多线程-1.10 暂停与恢复线程
- 当group by 和 order by 一起出现时
- 当借口出现时,想想当初的梦想!
- 从头认识多线程-4.2 对象逸出的其他解决方案-主要是ThreadLocal的应用
- 从头认识java-17.1 多线程
- LeetCode 002 Add Two Numbers
- Swift快速入门之协议与扩展
- xUtils AndroidStudio安装
- Android开发本地及网络Mp3音乐播放器(九)音乐收藏与列表切换
- 为什么要设置java环境变量
- 从头认识多线程-2.6 当异常出现时,线程自动释放锁
- <读书笔记>软件调试之道 :问题的核心-重现问题
- 诊断Java代码中常见的数据库性能热点问题应该这么做!
- Style input element to fill remaining width of its container
- Android杂记(3)FileUtils工具类梳理
- 欢迎使用CSDN-markdown编辑器
- php之自定义session存储机制【3】
- Keeloq解码软件实现
- typedefine 语法