finalised java_Running while loop infinitely without any code inside in java

可以将文章内容翻译成中文,广告屏蔽插件会导致该功能失效:

问题:

Lets say I have written a infinite write loop but didn't have statement inside it? Will it create any issue like memory will be full etc or JVM will stop responding after sometime?

回答1:

Why would you do something like that?

To answer, it wouldn't consume endless memory but Cpu usage could be a pain with really no instruction at all.

At minimum, you should help CPU preemption allowing the Thread to yield:

Thread.yield();

You can read this in Java Api Javadoc:

A hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore this hint.

Yield is a heuristic attempt to improve relative progression between threads that would otherwise over-utilise a CPU. Its use should be combined with detailed profiling and benchmarking to ensure that it actually has the desired effect.

It is rarely appropriate to use this method. It may be useful for debugging or testing purposes, where it may help to reproduce bugs due to race conditions. It may also be useful when designing concurrency control constructs such as the ones in the java.util.concurrent.locks package.

回答2:

An infinite loop might and probably will result in 100% CPU core utilization. Depending what you mean by "write loop" a similar technique is called Busy Waiting or Spinning.

spinning as a time delay technique often produces unpredictable or even inconsistent results unless code is implemented to determine how quickly the processor can execute a "do nothing" loop, or the looping code explicitly checks a real-time clock

回答3:

You'll certainly keep one hardware thread busy. It wont create any objects, so memory isn't a direct issue as such.

However, the context is important.

If it is a high priority thread, the system may become unresponsive. This is implementation specific. Twenty years ago I wrote an infinite loop that made a Windows NT system unresponsive. (I think this was a TCP proxy and only happened when an IBM 3090 running CICS sent an empty keep alive frame to a 3270 terminal. Good times.)

If the thread is holding any locks, that wont be released.

If the thread does something useful, that useful thing wont happen. For instance if you were to write the loop in a finaliser (and the system only has one finaliser thread), no other object will get finalised and therefore not garbage collected either. The application may behave peculiarly. It'salways fun to run random code on the finaliser thread.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于这些Budget aims/goals The current budget aim is to produce a solution while staying well under our project prototyping budget of $350. We aim to keep our budget below 50% of the $350, however this will only be achievable in the development phase because of savings of borrowed items from Shoalhaven Water. Final costings will be dependent on housing construction and will be decided after further discussion with Shoalhaven Water. Budget Estimate Most hardware components in the design are finalised for both the power system and housing, compiled in a full list, sourced, and budgeted. Changes to Previous Estimate: Updated the selected battery from the 6000mAh LIPO ($31) to 2x parallel 18650 2600mAh with battery holders ($46 + $7). Updated solar power manager and separate solar panel cost ($8 and $12) to a combined solar panel and power manager ($50) This solar power manager added an optional 5v input and removed the need for the voltage regulator ($5). Added two Stainless Steel U Bolts ($10) and O-ring gasket material ($5) for housing. The estimated total was updated from $273 to $340. Project Costing Hardware The supplied components include the mDot LoRaWAN device ($74), the voltage pulse reed switch ($93) (water meter and standpipe are already used/in field). The device cost will only consider the hardware involved. It will utilise the Shoalhaven Water pre-existing LoRaWAN network as well as their backend infrastructure for data storage and analysis which are additional costs that will not be accounted for in the budget of this project. The estimated cost of the device hardware per unit is $340. The full list of device hardware expenses is detailed below in table VIII. For the project prototype, Shoalhaven Water are supplying the LoRaWAN mDot, voltage pulse reed switch, standpipe and water meter, making the actual hardware expenses during the project just $173. This means that our design is $177 below our required project budget.
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值