从头认识多线程-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



0 0