0

I have a problem I don't find how to solve. I have a main thread running two nested loops. The nested loop is executing business rules and checking a certain condition to know when it breaks.

The problem here is this condition is time expensive and the goal of main program is to measure the time it takes, so checking this condition again and again introduces a skewness into the time measure.

I think I could put the checker into an independent thread and when it is satisfied, notify to the main thread and get out from the nested loop. Is it possible?

Thanks

4 Answers4

0

Yes, it is possible and not that difficult. All you have to do is to look into the future... (pun intended). But seriously look into class Future. Read about it in the API (provided link) and may be search for usage examples, usually in conjunction with ExecutorService. This is exactly what you need.

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • I think it is not the solution, I can't paste an example because of space scarce: You can see the main thread is waiting the result of the callable thread. I need the main thread active all the time whereas the callback checks the conditions and when it is satisfied is notified to the main thread no matter which instruction is in execution in that moment and so it forces the main thread to execute a part of code... – Carlos M. Fernandez Jul 29 '19 at 20:10
  • Carlos, your main thread can keep looping and poll the future once per loop. It doesn't have to block. @ Prashant Pandey - yes with this solution he has to poll, but it doesn't block the thread as the Future can tell you immediately if it is done or not. And yes, there are other ways to do things through notifications, but this is the simplest way – Michael Gantman Jul 29 '19 at 21:55
0

Yes its possible. All you have to do is install third party plugin available for vs code or sublime whichever you prefer. You can check this on internet. Then run your program this will show you exact time taken by your program. For this testing purpose I suggest you run only one single thread you want to test.

Ryan Schaefer
  • 3,047
  • 1
  • 26
  • 46
0

If you are doing this to measure the code performance/performance tuning go for JMH.
If you want to monitor you code block performance in runtime go for JFR or monitoring solution like Prometheus.
If you have some use case which is dependent on duration of execution of this method then you can use CompletableFutures where main thread will start the code blocks it's dependent on and you can pass the callback which will be executed upon completion of all methods.

Sumit Dhaniya
  • 596
  • 6
  • 14
0

I don't think this is possible. You can control the thread-pool that executes your callback, but not the exact thread. They do something like this in Android though:

Running code in main thread from another thread

On a side-note, the event loop in Javascript does what you want - takes the callbacks from the task queue, and pushes it to the main thread's call stack.

Prashant Pandey
  • 4,332
  • 3
  • 26
  • 44