启动/暂停/恢复/暂停...由其他类调用的方法 [英] Start/Suspend/Resume/Suspend ... a method invoked by other class

查看:416
本文介绍了启动/暂停/恢复/暂停...由其他类调用的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个Anytime k-NN分类器,但我找不到一种方法来调用classify(...)方法一段特定的时间,暂停它,在方法暂停之前获取可用的结果,恢复该方法一段特定的时间,暂停它,在方法暂停之前获得可用的结果,等等...
我使用数据结构来获得近似结果。当算法遍历数据结构时,它最终会遇到实际的训练数据向量。

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 before the method was suspended, resume the method for a specific amount of time, suspend it, get the available results before the method was suspended, and so on... I use a data structure for getting approximate results. While the algorithm traverses the data structure, it will encounter the actual training data vector, eventually.

public class AnytimeKNN{
 public int classify(queryPoint, k){
   class_label;
   1. Assign an initial value to 'class_label'.
   2.while(not actual training data vectors are encountered){
     1. traverse the data structure
     2. assign a new value to 'class_label'
    }
  }
}

我想从a调用'classify(..)'方法以下列方式的主要方法:

I want to call the 'classify(..)' method from a main method in the following manner:


  • 启动方法'classify(..)'

  • 当分配初始值为'class_label'时暂停方法'classify(..)'。

  • 获取初始标签

  • 继续方法'为X时间分类(..)'

  • 暂停方法'classify(..)'

  • 获取新的'class_label'

  • 恢复方法'分类(..)'X时间
    等等......

  • Start the method 'classify(..)'
  • Pause the method 'classify(..)' when initial value to 'class_label' is assigned.
  • Get the initial label
  • Continue the method 'classify(..)' for X amount of time
  • Pause the method 'classify(..)'
  • Get the new 'class_label'
  • Resume the method 'classify(..)'for X amount of time an so on....

提前致谢!

推荐答案

听起来像并发编程中的典型生产者 - 消费者场景。在Java中,您可以使用两个二进制信号量来解决这个问题。一个告诉分类器它应该运行,一个告诉主线程得到下一个结果。分类器等待其信号量,直到它被主线程触发。主线程的行为类似。

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.

这篇关于启动/暂停/恢复/暂停...由其他类调用的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆