Detailed Java thread priority and deadlock _java

Source: Internet
Author: User

Java Thread Priority
The special type of error that needs to be avoided with multitasking is deadlock (deadlock). Deadlocks occur when two threads have a circular dependency on a pair of synchronized objects. For example, suppose one thread enters the enhancement of object X and another thread enters the object Y. If the thread of X attempts to invoke the synchronization method of Y, it will be locked as expected. And Y's thread also wants to call some of the sync methods of x, and the thread waits forever, because to reach X, you have to release your own y lock so that the first thread can complete. Deadlock is a difficult error to debug because:
Typically, it occurs rarely and occurs only if the time period of the two threads matches exactly.
It may contain more than two threads and synchronization objects (that is, deadlocks can occur when there are more complex sequence of events than the example just described).

In order to fully understand the deadlock, it is useful to observe its behavior. The following example generates two classes, A and B, respectively, with the Foo () and Bar () methods. Both of these methods have a short pause before invoking the methods of other classes. The main class, named Deadlock, creates an instance of a and B, and then launches a second thread to set the deadlock environment. The foo () and Bar () methods use the sleep () forced deadlock phenomenon.

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) {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" thread.
  System.out.println ("Back in Main thread");
    public void Run () {B.bar (a);//Get lock on B in the other thread.
  System.out.println ("Back in the 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 to call
trying () B.last
Racingthread To call A.last ()

Because the program is deadlocked, you need to press CTRL-C to end the program. Press Ctrl-break on your PC (or press ctrl-\ under Solaris) to see the full range and the tube buffer heap. You will see that racingthread occupies the tube B while waiting for the pipe A, while Mainthread occupies a wait for B. The program will never end. As this example illustrates, your multithreaded routines are often locked, and deadlocks are the first thing you should check.

Java thread deadlock
The special type of error that needs to be avoided with multitasking is deadlock (deadlock). Deadlocks occur when two threads have a circular dependency on a pair of synchronized objects. For example, suppose one thread enters the enhancement of object X and another thread enters the object Y. If the thread of X attempts to invoke the synchronization method of Y, it will be locked as expected. And Y's thread also wants to call some of the sync methods of x, and the thread waits forever, because to reach X, you have to release your own y lock so that the first thread can complete. Deadlock is a difficult error to debug because:
Typically, it occurs rarely and occurs only if the time period of the two threads matches exactly.
It may contain more than two threads and synchronization objects (that is, deadlocks can occur when there are more complex sequence of events than the example just described).

In order to fully understand the deadlock, it is useful to observe its behavior. The following example generates two classes, A and B, respectively, with the Foo () and Bar () methods. Both of these methods have a short pause before invoking the methods of other classes. The main class, named Deadlock, creates an instance of a and B, and then launches a second thread to set the deadlock environment. The foo () and Bar () methods use the sleep () forced deadlock phenomenon.

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) {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" thread.
  System.out.println ("Back in Main thread");
    public void Run () {B.bar (a);//Get lock on B in the other thread.
  System.out.println ("Back in the 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 to call
trying () B.last
Racingthread To call A.last ()

Because the program is deadlocked, you need to press CTRL-C to end the program. Press Ctrl-break on your PC (or press ctrl-\ under Solaris) to see the full range and the tube buffer heap. You will see that racingthread occupies the tube B while waiting for the pipe A, while Mainthread occupies a wait for B. The program will never end. As this example illustrates, your multithreaded routines are often locked, and deadlocks are the first thing you should check.

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.