Threading - why do we use while (true) while waiting

Most thread examples in Java typically use a while (true) block as follows:

while(true) { try { wait() } catch (Exception ex) { /*do something*/ } } 

What is the purpose of using while (true)? In which scenarios are they especially useful? Communication between client and server?

Thanks,
- Ivar

+6
source share
5 answers

while (true) is useful if you want to do something all the time while your code is running, and you don’t know how often you should do it.

Client / server communication is one of the scenarios. Other scenarios are animating games or constantly saving some values.

A Thread is separated from other code, so your application does not freeze using while (true).

It is useful to add the exit flag to your while (true) -loop, so you can stop it if you want.

 Boolean isExit = false; //set this value to true when needed. while(true) { try { if(isExist){ break; } wait() } catch (Exception ex) { /*do something*/ } } 
+1
source

This type of construct is used when we create thread pools and reusable threads. In principle, the while(true) construct prevents pool threads from exiting.

An example is the situation with the producer-consumer, where Threads wait() until the queue is empty, and as soon as the queue has data, they will notify() and one of the threads will consume data from the queue and process it, and then returns to wait() for more data.

+4
source

Here's how you implement a condition variable in Java:

 synchronized (this) { while (true) { try { wait(); if (someCondition) { break; } } catch (InterruptedException ex) { // handle the 'interrupt' case ... } } // Do something that depended on the condition being true. } 

The wait call waits until some other thread does notify on this , presumably to say that the condition is now true. However, it is possible that the condition became false before the current thread woke up or that several threads were woken up. (Obviously, this depends on the application logic of what causes notify or notifyAll .) So, we (re) check the condition and repeat if this is not true. This is the while (true) point.

The wait() call must be made inside the monitor lock on this , and it is assumed that this lock protects the state that someCondition checks.

Another tricky thing is related to InterruptedException , which can be thrown by wait() . Some people think this is normal, just crush it. In fact, the right thing is to either distribute it or call Thread.interrupt() to set the interrupted flag again and stop everything that the current thread is doing. If you do not, the thread effectively ignores interrupts ... which is bad.


FWIW - catching an Exception , not an InteruptedException - this is bad practice and possibly a mistake.

+1
source

A true value causes the endless loop to stop the flow terminating, for example, you do not want your server to wait for clients to connect only for verification, and then to stop the verification, you want the server to continue checking over. Using a while loop allows break; exit the loop if something goes wrong.

0
source

This is an infinite loop, so the thread will wait indefinitely until an exception occurs (most likely an InterruptedException ).

This example does not make much sense on its own, usually you do something after wait (), and then wait for the next notification from another thread. Then you should use while(true) to wait endlessly for a notification, do some things, and then wait until the application throws an exception or some condition is met to interrupt the loop.

0
source

Source: https://habr.com/ru/post/891430/


All Articles