Start/Suspend/Resume/Suspend … a method invoked by other class

前端 未结 1 530
长情又很酷
长情又很酷 2021-01-26 21:35

I want to implement an Anytime k-NN classifier but I cannot find a way to call the \"classify(...)\" method for a specific amount of time, suspend it, get the available results

相关标签:
1条回答
  • 2021-01-26 21:57

    Sounds like a typical producer-consumer scenario in concurrent programming. In Java, you could solve this using two binary semaphores. One that tells the classifier that it should run and one that tells the main thread to get the next result. The classifier waits on its semaphore until it is triggered by the main thread. The main thread behaves similar.

    Of course, there would be other options e.g. using a concurrent queue. The classifier puts its result into the queue and the main thread pulls them out, waiting if there is no new result. This would be my favorite solution but perhaps you have a reason why you want to start and stop the method in fixed time intervals.

    0 讨论(0)
提交回复
热议问题