But what if I want to kill the thread from If you do this, make sure stop_running uses the volatile keyword. I will cover both while loop versions in this text.. Converting While Loop To For Loop; While(true) Statement In Sockets Programming; Send Message To All Connected Clients; Infinite While Loop; Sending Name Of Client To Server And Server Repeats It Back. It means that the person trying to read the code has to look elsewhere for the reason that the loop is terminated. You can use a flag say:Later, make sure that isRunning is set to false after you are done reading from the buffer or data file.If I were do what you are talking about I would try this:This allows you to make sure that you don't get any concurrent modification exepction on your messageQueue, as well as when there is no message you will not be using CPU time in the while(true) loop.
It's better to have the termination condition on the But maybe you're asking about what should go inside the loop. site design / logo © 2020 Stack Exchange Inc; user contributions licensed under
By invoking this method within each iteration of a spin-wait loop construct, the calling thread indicates to the runtime that it is busy-waiting. It is from the differences that you can see why C++ should be an alternative to Java. Maintenant, vous avez juste besoin de faire en sorte que lorsque vous ajoutez quelque chose à votre messageQueue vous pouvez appeler Java+You, Download Today!. "while (true)" is a kludgy way of saying that the test that exits the loop is either going to be made within the loop (via a "break" statement, return, Exception, or whatever) or if all else fails, because the application is forcibly cancelled either from the … Infinite loop means a loop that never ends. before checking if the condition is true, then it will repeat the loop as long This is typically I usually go with a class attribute boolean called 'done', then the threads' run methods look likeYou can then set done=true to kill the loop. Consumers should process these messages as fast as possible!Instead of looping forever and breaking or returning, you might choose to check the interrupted status.If your threads are tasks managed by an ExecutorService, you can have them all end gracefully simply by calling shutdownNow()....perhaps? Vous devez assurez-vous d'inclure certaines opération de blocage, c'est à dire, certains appel de fonction de l'endroit où votre fil d'attente pour quelqu'un d'autre (un autre fil, un autre programme, le système d'exploitation) à faire quelque chose. This can be done from inside the loop, or you can have another method that sets it, so that other threads can pull the plug.It looks like you are busy waiting, assuming a standard First up, the straight answer to this problem by Dough Lea:It is almost never a good idea to use bare spins waiting for values of variables. Just call take instead of poll if you want to block while the queue is empty. – pasaba por aqui Mar 17 '16 at 16:19 zb ein lockfile, das man vorher erstellt, nachher löscht und bei jedem durchlauf einmal auf existenz prüft. Now you just need to make sure that when you do add something to your messageQueue you can call Thanks for contributing an answer to Stack Overflow! Unless it's something like what Mythios mentioned (endless loop for message processing), I personally think using while (true) is very sticky unless you are guaranteed that a condition will be met to break out of the loop.
Par l'invocation de cette méthode au sein de chaque itération d'un spin-boucle d'attente construire, le thread appelant indique à l'exécution qu'elle est occupée d'attente. » Uninstall About Java
One is the condition of processing, the other is the condition of staying in the loop.
Free 30 Day Trial The reason you check for interruption in the while() statement is to provide a means of ending the thread in a graceful manner. Si je devais faire ce que vous parlez, je voudrais essayer ceci:Cela vous permet de vous assurer que vous n'obtenez pas de modifications simultanées exepction sur votre messageQueue, ainsi que lorsqu'il n'y a pas de message, vous ne serez pas en utilisant le temps CPU dans le while(true) en boucle. W3Schools is optimized for learning, testing, and training. The Java while Loop. In terms of speed, C++ is relatively faster than Java. Chaque thread consommateur est en boucle, la vérification d'un message s'affiche dans la file d'attente et de la traiter.Le Producteur est en train de mettre des messages à l'intérieur des Consommateurs files d'attente de messages à un rythme rapide (plusieurs millions de messages par seconde).
You need to make sure to include some blocking operation, i.e., some function call where your thread will wait for someone else (another thread, another program, the OS) to do something. Qui peut le rendre moins lisible.Cette mentalité est poussée à l'extrême les résultats dans le COMEFROM mot-clé. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. Si vous venez de faire une boucle autour et autour de la vérification d'une condition, votre thread va manger toute la CPU ne rien faire. The Overflow Blog