Java multithreading: thread deadlock

Source: Internet
Author: User

deadlocks are generally caused by mutual lock wait between two objects.
The following example is used to construct this situation:

Package basic. e_deadlock; import Org. apache. log4j. logger; public class testdeadlock {public static void main (string [] ARGs) {deadlockrisk dead = new deadlockrisk (); mythread T1 = new mythread (dead, 1, 2, "thread 1"); mythread t2 = new mythread (dead, 3, 4, "thread 2"); mythread T3 = new mythread (dead, 5, 6, "thread 3"); mythread t4 = new mythread (dead, 7, 8, "thread 4"); t1.start (); t2.start (); t3.start (); t4.start () ;}} class MYT Hread extends thread {private deadlockrisk dead; private int A, B; mythread (deadlockrisk dead, int A, int B, string threadname) {This. dead = dead; this. A = A; this. B = B; this. setname (threadname) ;}@ overridepublic void run () {dead. read (); dead. write (a, B) ;}} class deadlockrisk {Private Static logger = logger. getlogger (deadlockrisk. class); Private Static class resource {public int value;} private reso Urce resourcea = new resource (); private resource resourceb = new resource (); Public void read () {logger. debug ("============= read begin ============"); synchronized (resourcea) {logger. debug ("Read ():" + thread. currentthread (). getname () + "gets the resourcea lock! "); Synchronized (resourceb) {logger. debug (" Read (): "+ thread. currentthread (). getname () +" gets the resourceb lock! ") ;}} Logger. debug ("============= read end ==============");} public void write (int, int B) {logger. debug ("============= write begin ============"); synchronized (resourceb) {logger. debug ("write ():" + thread. currentthread (). getname () + "gets the resourceb lock! "); Synchronized (resourcea) {logger. debug (" write (): "+ thread. currentthread (). getname () +" gets the resourcea lock! "); Resourcea. value = A; resourceb. value = B ;}} logger. debug ("============= write end ============== ");}}

execution result:

0 [thread 1] Debug basic. e_deadlock.deadlockrisk-=========== read begin =========== 0 [thread 2] Debug basic. e_deadlock.deadlockrisk-=========== read begin =========== 0 [thread 4] Debug basic. e_deadlock.deadlockrisk-=========== read begin =========== 0 [thread 3] Debug basic. e_deadlock.deadlockrisk-=========== read begin =========== 0 [thread 1] Debug basic. e_deadlock.deadlockrisk-read (): thread 1 gets the resourcea lock! 0 [thread 1] Debug basic. e_deadlock.deadlockrisk-read (): thread 1 gets the resourceb lock! 0 [thread 1] Debug basic. e_deadlock.deadlockrisk-============= read end ================= 0 [thread 2] Debug basic. e_deadlock.deadlockrisk-read (): thread 2 obtains the resourcea lock! 0 [thread 1] Debug basic. e_deadlock.deadlockrisk-============ write being ============= 1 [thread 2] Debug basic. e_deadlock.deadlockrisk-read (): thread 2 obtains the resourceb lock! 1 [thread 2] Debug basic. e_deadlock.deadlockrisk-============= read end ================= 1 [thread 2] Debug basic. e_deadlock.deadlockrisk-============ write being ============= 1 [thread 1] Debug basic. e_deadlock.deadlockrisk-write (): thread 1 gets the resourceb lock! 1 [thread 4] Debug basic. e_deadlock.deadlockrisk-read (): thread 4 gets the resourcea lock!

Note: thread 1 is waiting for resourceb resources, and thread 2 is waiting for resourcea resources. The two threads are waiting for each other and there is a deadlock.

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.