Problem Description: I have a program that is multi-threaded to call the external interface to get data, and now the other party only supports 6000 times / minute access. If the number of visits exceeds, there is no data. If the number of threads is reduced, it will not be exceeded. However, the program requires higher efficiency. I want to maximize the speed of the program. I want to design my code to control access within one minute The number of times (such as 5800 times), tried a lot of methods, check a lot of information or can not be achieved. Is there an implementation plan or framework for this?
Asked
Active
Viewed 71 times
-1
-
Can you share your code? What methods did you try? – Jay Jul 12 '19 at 18:09
-
If the action is IO bound, adding more threads won't help. Otherwise, you can have 6k requests in 1 minute, which means the most you can do 100 requests every second. If each request took 5ms, you could fit 200 requests in a second. Since 200 would result in half the requests not returning data, slow it down manually via some `sleep` system based on the delta/elapsed time. With knowing how many requests a single thread can handle, you can split the requests across threads accordingly to meet your needs. – Vince Jul 12 '19 at 18:11
-
1Search for `java throttle` – Andreas Jul 12 '19 at 18:40
-
Are you currently polling 6000 (or more) times/sec for _the same data_ or what is your use-case? Also, what do you mean with "higher efficiency"? – Mick Mnemonic Jul 12 '19 at 19:49
-
Thank you very much, I solved the problem according to the guidelines of @Andreas. Stackoverflow has a similar problem。https://stackoverflow.com/questions/1407113/throttling-method-calls-to-m-requests-in-n-seconds – liang Jul 14 '19 at 10:10
1 Answers
0
The rxjava framework supports all the operators that https://rxmarbles.com/ has to offer. You could probably use that: by creating an Observable that receives dummy events.
Each dummy event triggers such a rest call. And you use the appropriate rxjava operators to generate these events according to your timing requirements.
Sure, that is like the opposite of how you normally use that technology. But the fact is that these operators are extremely powerful and well tested. And you can even (easily) control which threads will receive the dummy events!

GhostCat
- 137,827
- 25
- 176
- 248
-
Thank you very much, I solved the problem according to the guidelines of @Andreas – liang Jul 14 '19 at 10:10