Learn from the beginning multithreading-2.5 When an exception occurs, the thread automatically releases the lock

Source: Internet
Author: User

In this chapter, let's discuss the following scenario: When an exception occurs, the thread automatically releases the lock.

1. General Information

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

Output:

Service_1 beginservice_1 workingservice_1 endservice_2 beginservice_2 workingservice_2 End

From the output and the previous article we can know, the above situation is which thread first get the lock, first executes, then executes, only to the next thread to get the lock, so the output is a method of the output of a method


2. We add the case of artificial anomalies

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"); {//artificial exception 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");}}

Output:

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


From the above output can be seen, when the exception of thread 0, immediately after the start of thread 1 execution, compared to the above results, thus, when an exception occurs, thread 0 has released the lock, let the other threads get the lock to continue execution.


Summary: In this chapter we discuss the case where the thread automatically releases the lock when an exception occurs.


This chapter is here, thank you.

------------------------------------------------------------------------------------

My github:https://github.com/raylee2015/deepintothread.


Catalog: http://blog.csdn.net/raylee2007/article/details/51204573



Learn from the beginning multithreading-2.5 When an exception occurs, the thread automatically releases the lock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.