Home>

java thread priority

A special type of error related to multitasking that needs to be avoided is deadlock. Deadlocks occur when two threads have a circular dependency on a pair of synchronization objects.For example, suppose one thread enters the management of object x and another thread enters the management of object y. If the thread of x tries to call the synchronous method of y,It will be locked as expected.The thread of y also wants to call some synchronization methods of x,The thread waits forever,Because to reach x, you must release your lock on y so that the first thread can complete.Deadlocks are bugs that are hard to debug,because:

Usually, it rarely happens,It can only happen when the time period of the two threads coincides.

It may contain more than two threads and synchronization objects (that is,Deadlocks can occur when there are more complex sequences of events than the example just described).

To fully understand deadlocks,It is useful to observe its behavior.The following example generates two classes,a and b have foo () and bar () methods, respectively. These two methods have a short pause before calling methods of other classes.The main class, named deadlock, creates instances of a and b, and then starts a second thread to set up the deadlock environment.The foo () and bar () methods use sleep () to force a deadlock.

//an example of deadlock.
class a {
  synchronized void foo (b b) {
    string name=thread.currentthread (). getname ();
    system.out.println (name + "entered a.foo");
    try {
      thread.sleep (1000);
    } catch (exception e) {
      system.out.println ("a interrupted");
    }
    system.out.println (name + "trying to call b.last ()");
    b.last ();
  }
  synchronized void last () {
    system.out.println ("inside a.last");
  }
}
class b {
  synchronized void bar (a a) {
    string name=thread.currentthread (). getname ();
    system.out.println (name + "entered b.bar");
    try {
      thread.sleep (1000);
    } catch (exception e) {
      system.out.println ("b interrupted");
    }
    system.out.println (name + "trying to call a.last ()");
    a.last ();
  }
  synchronized void last () {
    system.out.println ("inside a.last");
  }
}
class deadlock implements runnable {
  a a=new a ();
  b b=new b ();
  deadlock () {
    thread.currentthread (). setname ("mainthread");
    thread t=new thread (this, "racingthread");
    t.start ();
    a.foo (b);//get lock on a in this thread.
    system.out.println ("back in main thread");
  }
  public void run () {
    b.bar (a);//get lock on b in other thread.
    system.out.println ("back in other thread");
  }
  public static void main (string args []) {
    new deadlock ();
  }
}

After running the program,The output is as follows:

mainthread entered a.foo
racingthread entered b.bar
mainthread trying to call b.last ()
racingthread trying to call a.last ()

Because the program is deadlocked,You need to press ctrl-c to end the program.Press ctrl-break on the pc (or press ctrl- \ under solaris) and you can see the full thread and pipe buffer heap.You will see that racing thread occupies process b while waiting for process a, while mainthread occupies a and waits for b. The program never ends.As the example illustrates,Your multi-threaded program is often locked,Deadlock is the first thing you should check.

java thread deadlock

A special type of error related to multitasking that needs to be avoided is deadlock. Deadlocks occur when two threads have a circular dependency on a pair of synchronization objects.For example, suppose one thread enters the management of object x and another thread enters the management of object y. If the thread of x tries to call the synchronous method of y,It will be locked as expected.The thread of y also wants to call some synchronization methods of x,The thread waits forever,Because to reach x, you must release your lock on y so that the first thread can complete.Deadlocks are bugs that are hard to debug,because:

Usually, it rarely happens,It can only happen when the time period of the two threads coincides.

It may contain more than two threads and synchronization objects (that is,Deadlocks can occur when there are more complex sequences of events than the example just described).

To fully understand deadlocks,It is useful to observe its behavior.The following example generates two classes,a and b have foo () and bar () methods, respectively. These two methods have a short pause before calling methods of other classes.The main class, named deadlock, creates instances of a and b, and then starts a second thread to set up the deadlock environment.The foo () and bar () methods use sleep () to force a deadlock.

//an example of deadlock.
class a {
  synchronized void foo (b b) {
    string name=thread.currentthread (). getname ();
    system.out.println (name + "entered a.foo");
    try {
      thread.sleep (1000);
    } catch (exception e) {
      system.out.println ("a interrupted");
    }
    system.out.println (name + "trying to call b.last ()");
    b.last ();
  }
  synchronized void last () {
    system.out.println ("inside a.last");
  }
}
class b {
  synchronized void bar (a a) {
    string name=thread.currentthread (). getname ();
    system.out.println (name + "entered b.bar");
    try {
      thread.sleep (1000);
    } catch (exception e) {
      system.out.println ("b interrupted");
    }
    system.out.println (name + "trying to call a.last ()");
    a.last ();
  }
  synchronized void last () {
    system.out.println ("inside a.last");
  }
}
class deadlock implements runnable {
  a a=new a ();
  b b=new b ();
  deadlock () {
    thread.currentthread (). setname ("mainthread");
    thread t=new thread (this, "racingthread");
    t.start ();
    a.foo (b);//get lock on a in this thread.
    system.out.println ("back in main thread");
  }
  public void run () {
    b.bar (a);//get lock on b in other thread.
    system.out.println ("back in other thread");
  }
  public static void main (string args []) {
    new deadlock ();
  }
}

After running the program,The output is as follows:

mainthread entered a.foo
racingthread entered b.bar
mainthread trying to call b.last ()
racingthread trying to call a.last ()

Because the program is deadlocked,You need to press ctrl-c to end the program.Press ctrl-break on the pc (or press ctrl- \ under solaris) and you can see the full thread and pipe buffer heap.You will see that racing thread occupies process b while waiting for process a, while mainthread occupies a and waits for b. The program never ends.As the example illustrates,Your multi-threaded program is often locked,Deadlock is the first thing you should check.

  • Previous Method for solving garbled parameters of java page URL address transmission parameter
  • Next Summary of PHP code optimization tips