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.