java - Why deadlock is occuring intermittently in my code? -


is

Below I wrote to a deadlock, but do not fall into the "code deadlock for" loop small, When I keep "loop" for 10 then there is a catastrophe.

Is someone showing such behavior that why is he showing such behavior?

  public class CustomerUpdateDeadloackThread {public static zero main (string [] args) {customer CSTMR = new customer "Peter"); Address adrs = new address ("B-232, Bangalore"); // do not show deadlock for Loop 3 (Int i = 0; i & lt; 10; i ++) for (new Thread (new Tag object Tueac other (Sistimar, Aders)). Start (); new Thread (new tag object Tueacadik (Aders, Sistimar)). start ();}}} interface client updater {public boolean update (object obj);} class Tags implements object Tueakany Rnnabl {Grahkapdetr Taskitm object objToUpdateWith; public TagObjectsToEachOther (CustomerUpdater cspdtr, Objects Crop obj2) {this.taskItem = cspdtr; This.objToUpdateWith = obj2;} @ Override public wide run () {taskItem.update (objToUpdateWith); System.out.println ( "worked" + thread curentthread (). GetName ());}} class address implements Grahkapdet {string address; client; public address (string Aderrs) {this.address = addrs;} @ override public boolean update (object Sistimar) {synchronized (this) {synchronized (( Customer) CSTMR {try.customer = (Customer) CSTMR; Thread.Sleep (2000); // Otherwise doing some other work here Sector are} catch (CustomerUpdateFailureException e) {e.getCause (); return false; } Grip (Interrupted E) E. {// TODO Auto-Generated Catch Block e.printStackTrace (); } Back true; }}}} Class customer implements CustomerUpdater {String name; Address address; Public customer (string nm) {this.name = nm; } @ Override Public Boolean Update (Object Ads) {Synchronize (This) {Synced (Address) ads) {try {this.address = (Address) ads; Thread.Sleep (2000); // otherwise do some other work here; hold (CustomerUpdateFailureException e) {e.getCause (); return false; } Grip (Interrupted E) E. {// TODO Auto-Generated Catch Block e.printStackTrace (); } Back true; }}}} Category CustomerUpdateFailureException RuntimeException Extended {Private Stable last long serialVersionUID = 1L; @ Override public string let message () {return "incompatible update"; }}  

You are only going to receive a deadlock if you receive a thread One monitor receives another monitor and other threads before the first thread receives another monitor, as threads keep track of two objects, the more likely it is that one of them Just get a lock and get another chance to get a monitor May first be pre-cleared.

In other words, it's okay, and just causes a wait:

  Thread A Thread B Lock X lock car lock Y / blocks blocks (temporary) Sleep lock x slip  

While this causes the deadlock:

  Thread Thread B. Lock X Lock Y Lock Y // Block (Deadlock) Lock X // Block (Deadlock)  

If you call your thread. (2000) , then your two synchronized Statement Call between s methods) you almost


Comments

Popular posts from this blog

HTML/CSS - Automatically set height width from background image? -

php - Mysql Show Process - Sleep Commands and what to do -

c - What is the address of buf (the local variable in the main function)? -