What is the consequence of a thread executing a dead loop in Java _java

Source: Internet
Author: User

Suppose there are two threads running concurrently, and one thread executes code that contains a dead loop such as: while (true) .... When the thread executes code in while (true), does another thread have a chance to execute it?

Sample Code (code from the Internet)

public class Service {
  Object object1 = new Object ();

  public void MethodA () {
    synchronized (object1) {
      System.out.println ("MethodA begin");
      Boolean iscontinuerun = true;
      Executes a dead loop here
      (Iscontinuerun) {
        
      }
      System.out.println ("MethodA end");
    }

  Object object2 = new Object ();

  public void MethodB () {
    synchronized (object2) {
      System.out.println ("MethodB begin");
      System.out.println ("MethodB End");}}


The implementation of the two thread classes is as follows:

Import service. Service;

public class Threada extends Thread {

  Private service service;

  Public Threada (Service service) {
    super ();
    This.service = service;
  }

  @Override public
  Void Run () {
    Service.methoda ();
  }

}

Thread A executes MethodA (), and there is a dead loop in MethodA ()

Import service. Service;

public class Threadb extends Thread {

  Private service service;

  Public threadb (Service service) {
    super ();
    This.service = service;
  }

  @Override public
  Void Run () {
    service.methodb ();
  }

}

Thread B executes MethodB (), and when thread a enters the while dead loop in MethodA (), can thread B perform the execution?

Test class

Import service. Service;
Import Extthread. Threada;
Import Extthread. threadb;

public class Run {public

  static void Main (string[] args) {
    Service service = new service ();

    Threada athread = new Threada (service);
    Athread.start ();

    THREADB bthread = new threadb (service);
    Bthread.start ();
  }



Since thread A and thread B acquire an object lock that is not the same lock, it can be seen from the results that thread B is executable. Thread A, which has entered the while Dead loop, has been run by thread A (the entire program has not ended), but thread B will end.

That is, although thread A has been executing in the while, it needs to occupy the CPU. However, the scheduling of threads is the responsibility of the JVM or the operating system, not that thread A has been in the while loop, and thread B is not consuming the CPU. For thread A, it is equivalent to a "compute-intensive" job. If our while loop is constantly testing whether a condition is set up, this is a waste of CPU, referring to a specific example: the "Communication between Threads" 2nd while polling between Java multi-line threads.

If the Service.java is modified as follows:

public class Service {
//  Object Object1 = new Object ();

  public void MethodA () {
    synchronized (this) {
      System.out.println ("MethodA begin");
      Boolean iscontinuerun = true;
      Executes a dead loop here
      (Iscontinuerun) {
        
      }
      System.out.println ("MethodA end");
    }  Object object2 = new Object ();

  public void MethodB () {
    synchronized (this) {
      System.out.println ("MethodB begin");
      System.out.println ("MethodB End");}}


If thread a first obtains an object lock, thread A is in the while empty loop because of the while loop. Thread B does not perform methodb () because it cannot acquire a lock.

As you can see, if a thread cannot be exited in the synchronized method and the lock cannot be freed, the other thread can only wait indefinitely.

The above is the entire content of this article, I hope to learn Java multithreading help.

Related Article

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.