且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Java中使用while循环是否不安全?

更新时间:2023-01-16 21:29:16

软件

不,不是这样.只要您不会因保持锁引起的问题而使代码的其他部分死锁,并且您具有有效的停止条件,从而使用户不需要终止您的应用程序,那么您就可以了. 但是 ,如果您正在创建新对象并保留对它们的引用,则可能会耗尽内存.

Software

No, not really. As long as you don't cause issues with held locks deadlocking other parts of code and you have valid stopping conditions so the user doesn't need to kill your application you should be fine. However, if you are creating new objects and hold references to them you could run out of memory.

许多程序(包括Java)在CPU使用率和循环方面都有其应有的份额.游戏,其他编译器,甚至办公应用程序都可以做到这一点.实际上,许多程序都依赖于无限循环(尽管有停止条件,但它们可以是 while(true)类型.可以产生热量,但无需担心.

Many programs, including Java, have their fair share of CPU use and loops. Games, other compilers, and even office applications can do this. In fact, many programs rely on infinite loops(albeit with a stopping condition but they can be of the while(true) type. Heat can be generated but it's nothing to worry about.