首页 \ 问答 \ 调节方法调用使用Guava RateLimiter类(Throttling method calls using Guava RateLimiter class)

调节方法调用使用Guava RateLimiter类(Throttling method calls using Guava RateLimiter class)

我试图扼杀每秒调用方法的次数。 我试图用Guava RateLimiter来达到这个目的。

RateLimiter rateLimiter = RateLimiter.create(1.0);//Max 1 call per sec
rateLimiter.acquire();
performOperation();//The method whose calls are to be throttled.

然而,调用的方法不限于每秒1次,而是连续的。

使用Thread.sleep()可以实现节流,但是我希望使用Guava而不是sleep()。

我想知道使用Guava RateLimiter实现方法调用trottling的正确方法。 我检查了RateLimiter的文档并试图使用相同的文件,但无法达到预期的效果。


I am trying to throttle the number of calls to a method per second. I tried to achieve this using Guava RateLimiter.

RateLimiter rateLimiter = RateLimiter.create(1.0);//Max 1 call per sec
rateLimiter.acquire();
performOperation();//The method whose calls are to be throttled.

However the methods to the call are not limited to 1 per second but are continuous.

The throttling can be achieved using Thread.sleep() but i wish to use Guava rather that sleep().

I would like to know the right way to achieve the method call trottling using Guava RateLimiter. I have checked the documentation for RateLimiter and tried to use the same but could not achieve the desired result.


原文:https://stackoverflow.com/questions/31883739
更新时间:2022-10-28 15:10

最满意答案

渲染意味着加载,因此如果某些东西是渲染阻塞,则意味着它会尽可能快地加载页面。

1)在页眉中加载css但在页脚中加载Js

2)使用精灵图像

3)您可以添加htaccess代码以获得更好的性能

  • Gzip压缩
  • 设置Expires和Cache-Control标头
  • 浏览器缓存控制

4)在页面上仅加载所需的css / js

选择最佳方法一旦确定需要移动哪些脚本,就应该决定“如何”修复它们。 有两种主要方法可供选择。 第一个是使脚本内联; 在此方法中,脚本的内容将直接添加到页面的HTML中,并且只在需要时才加载。 如果脚本很小并且适用于单个页面,这是最佳选择。

另一种选择是推迟脚本。 当您推迟使用JavaScript时,您将延迟任何非必要脚本加载,直到第一次渲染之后或者直到加载了更重要的部分之后。 当脚本不重要且可以等待加载时,此方法最佳。

PS: - 在某些情况下,我们无法完全解决这个问题。

我希望它对你有所帮助


Render means loading, so if something is render-blocking, it means that it is keeping the page from loading as quickly as it could.

1) load css in the headers but Js in the footer

2) Use sprite images

3) You can add htaccess code for for better performance

  • Gzip compression
  • set Expires and Cache-Control headers
  • Browser cache-control

4) Load only required css/js on pages

Choose Best Method Once you’ve identified which scripts need to be moved it is time to decide “how” to fix them. There are two main methods to choose from. The first is to make the scripts inlined; in this method the contents of the script are added directly into the HTML of the page and are only loaded once they are needed. This is the best option if the script is small and applies to a single page.

Another option is to defer the script. When you defer JavaScript you are delaying any non-essential scripts from loading until after the first render or until after the more essential portions have loaded. This method is best when the script is not crucial and can wait to load.

PS:- We can not resolve this fully in some cases.

I Hope it's helpful for you

相关问答

更多
  • 所有CSS都是渲染阻止。 这个规则的唯一例外是你的DOM还不知道的CSS (加载异步,通过javascript构建/加载)。 直到您的浏览器没有通过构建CSS对象模型来解析(或认为它已解决)所有CSS ,该页面将不会呈现并且javascript将不会被执行。 但是, resolve并不一定意味着load 。 它可能意味着两件事: 加载和解析 。 如果它超过2k行代码,那么在使用适当的工具进行测量时,您会注意到它。 如果它超过10k行代码,您将能够在不使用任何工具的情况下注意到它。 无论是否包含有关元素上方元 ...
  • PageSpeed insight网站有一个很好的文档,您可以做些什么来防止它。 要使您的javascript无阻塞,您可以将async或defer属性添加到脚本标记中。 为了维护订单,建议使用defer 。 您还应该将其移动到关闭BODY标记之前,将“async”属性添加到标记。 它将通知浏览器在脚本可用时异步运行该脚本并删 ...
  • 是的,这几乎就是您推荐的页面所推荐的内容。 将最小量的CSS(只要少量)直接放在