One, the problem description
Assuming that there are two threads running concurrently, one thread executes code that contains a dead loop such as: while (true) .... When the thread executes the code in while (true), does the other thread have a chance to execute?
Two, sample code (code from the Internet)
1 Public classService {2Object Object1 =NewObject ();3 4 Public voidMethodA () {5 synchronized(object1) {6SYSTEM.OUT.PRINTLN ("MethodA Begin");7 BooleanIscontinuerun =true;8 //to perform a dead loop here9 while(iscontinuerun) {Ten One } ASystem.out.println ("MethodA End"); - } - } the -Object Object2 =NewObject (); - - Public voidMethodB () { + synchronized(object2) { -SYSTEM.OUT.PRINTLN ("MethodB Begin"); +System.out.println ("MethodB End"); A } at } -}
The implementation of the two thread classes is as follows:
1 Importservice. Service;2 3 Public classThreadaextendsThread {4 5 PrivateService service;6 7 PublicThreada (Service service) {8 Super();9 This. Service =Service;Ten } One A @Override - Public voidrun () { - Service.methoda (); the } - -}
Thread A executes MethodA (), there is a dead loop in MethodA ()
1 Importservice. Service;2 3 Public classThreadbextendsThread {4 5 PrivateService service;6 7 Publicthreadb (Service service) {8 Super();9 This. Service =Service;Ten } One A @Override - Public voidrun () { - Service.methodb (); the } - -}
Thread B executes MethodB (), and when thread a enters the while loop in MethodA (), can execution of thread B be completed?
Test class
1 Importservice. Service;2 ImportExtthread. Threada;3 ImportExtthread. threadb;4 5 Public classRun {6 7 Public Static voidMain (string[] args) {8Service service =NewService ();9 TenThreada Athread =NewThreada (service); One Athread.start (); A -THREADB Bthread =Newthreadb (service); - Bthread.start (); the } - -}
Execution Result:
Since thread A and thread B acquire an object lock that is not the same lock, it can be seen from the result that thread B can be executed. Thread A continues to run (the entire program is not finished) because it enters the while loop, but thread B ends.
That is, although thread A has been executing in while, it requires CPU usage. However, the scheduling of threads is the responsibility of the JVM or the operating system, not that thread A has been in while, and then thread B is not taking up CPU.
If the Service.java is modified as follows:
1 Public classService {2 //Object Object1 = new Object ();3 4 Public voidMethodA () {5 synchronized( This) {6SYSTEM.OUT.PRINTLN ("MethodA Begin");7 BooleanIscontinuerun =true;8 //to perform a dead loop here9 while(iscontinuerun) {Ten One } ASystem.out.println ("MethodA End"); - } - } the - //Object object2 = new Object (); - - Public voidMethodB () { + synchronized( This) { -SYSTEM.OUT.PRINTLN ("MethodB Begin"); +System.out.println ("MethodB End"); A } at } -}
If thread a obtains an object lock first, thread A has been in the while loop because of the while cycle. Thread B cannot execute MethodB () because it cannot get a lock.
As you can see, if a thread cannot exit in the Synchronized method, the lock cannot be freed, and the other thread can only wait indefinitely.
Java multi-thread when a thread executes a dead loop, does it affect another thread?