java同步机制
Web 应用程序查询外部服务器。如果过去“n”分钟内发送到服务器的请求中有超过 80% 失败,则 Web 应用程序应退出查询服务器并执行其他逻辑。我可以想到一个原子整数会因失败的请求而增加。但我不认为原子整数支持在原子整数的值达到特定值时执行的任何操作。在java中有没有更聪明的方法来做到这一点?
A web application queries an external server. If more than 80% of the requests to the server send within the past 'n' minutes fails, then the web applicatoin should back out from querying the server and perform other logic. I could think of an atomic integer being incremented by the failed requests. But i dont think atomic integer supports any action to be executed if the value of the atomic integer reaches a particular value. Is there any smarter way to do this in java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,更新原子整数后,您可以检查它的值,如果满足 80%,那么您可以采取行动(例如将该服务器标记为“弱响应等”)。如果您在多线程环境中工作,那么您的解决方案没有任何问题。
另一种解决方案是让线程调用同步方法来增加非原子整数并执行检查。该整数必须是该方法所属类的属性。
Well, after updating your atomic integer, you could check its value and if the 80% is met, then you could take action (like flagging that server as 'weak responding or so'). If you are working in a multi-threaded environment, there is nothing wrong with your solution.
Another solution is to have the threads call a synchronized method to increase a non-atomic integer and perform the check. That integer would have to be an attribute of the class to which this method belongs.
如果您想监视最后“N”分钟内的事件,您需要的不仅仅是一个整数。您需要知道“N”分钟前发生了什么,这样您才能保持正确的成功水平估计。
这是一种方法:
If you want to monitor events in the last 'N' minutes you need more than just an integer. You need to know what was happening 'N' minutes ago so you can keep your success level estimate correct.
Here is one way to do it: