0

I am doing calculations in milliseconds and I really do not want my thread to spend more time doing time calculations rather the job it is assigned to do. However I want to implement something that:

1- It should not generate more than n requests per second

2- If it has generated less, it should start at zero for the next second(obviously :D)

I am trying to do some performance benchmarking where my goal is to give all cpu to only processing and not time computations after every request. Roughly, I am processing 08:36 - 171299 08:37 - 170970 08:38 - 163763 I want to make sure I do not make more than 160000 requests per minute here. How to acheive that is the problem.

Thanks in advance!

Ani
  • 463
  • 4
  • 20
  • I think your machine (even if it is a GameBoy) can easily handle one calculation à la `1000 / n` per second. – bkis Sep 27 '17 at 13:06
  • You should really post some code, it's not clear what you're trying to do and how you're trying to do it. It sounds like you want to use a `ScheduledThreadPoolExecutor`. – daniu Sep 27 '17 at 13:07
  • @mumpitz I don't know what you've meant, I wouldn't call that helpful... – Filip Malczak Sep 27 '17 at 13:08
  • You should take a look at Guava's RateLimiter and search for examples on how to use it. – dpr Sep 27 '17 at 13:08
  • @FilipMalczak Thank you for your criticism, although it is in turn not very helpful. Despite my humorous wording, I actually tried to make a point: I don't think the calculation of the delays needed are a big problem. Your (honestly helpful) answer to this question does include the same calculation behind the scenes - which is fine, i think. It's a good way to deal with this anyway. Friends? – bkis Sep 27 '17 at 14:27
  • What happens with requests that span the second divider line? Or indeed, what is your original problem? Sounds to me like you've come up with a solution for a problem and now you're asking us to make your solution work rather than address the original problem. In essence this sounds like an X-Y problem. – biziclop Sep 27 '17 at 16:03
  • There are two parts, design and implementing. I am looking for all possible designs. I can implement it later. I still do not have the design in place to make this work. If I create threads on the fly with 1 sec timeout, I shall be spending lot of time in creation of threads. – Ani Sep 28 '17 at 06:15
  • @AnimeshJain I'm afraid that without you describing the problem you're trying to solve it's unlikely that you will get any useful answers. – biziclop Sep 28 '17 at 10:36
  • I am trying to do some performance benchmarking where my goal is to give all cpu to only processing and not time computations after every request. Roughly, I am processing TIME - No Of Requests 08:36 - 171299 08:37 - 170970 08:38 - 163763 I want to make sure I do not make more than 160000 requests per minute here. How to acheive that is the problem. – Ani Oct 04 '17 at 09:36

1 Answers1

0

You can combine ScheduledExecutorService to run some code every second and this answer to set timeout on that code. In the end, your runnable that should have 1-second timeout should generate up to n requests, and if it times out, it will start in next second with new context.

Filip Malczak
  • 3,124
  • 24
  • 44
  • Yes, that is a idea I had. I am looking for ways if I can use the same threadpool every time. Can I do that? or I need to timeout the thread every second and create a new one always ? – Ani Sep 27 '17 at 14:59
  • ScheduledExecutorService is still an ExecutorService - I think you can use the same pool to schedule the piece of code that you'll wrap in timeout and the real task itself. I am not sure of that though and I guess it may lead to some problems. Worst case scenario - keep two pools - one for scheduling, the other one for doing "real" work. – Filip Malczak Oct 10 '17 at 08:56