从头认识多线程-2.6 当异常出现时,线程自动释放锁

这一章节我们来讨论一下下面的情况:当异常出现时,线程自动释放锁。

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;
	}

	@Override
	public 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;
	}

	@Override
	public 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 begin
service_1 working
service_1 end
service_2 begin
service_2 working
service_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;
	}

	@Override
	public 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;
	}

	@Override
	public 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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值