【问题标题】:How to communicate between threads in Scala?如何在Scala中的线程之间进行通信?
【发布时间】:2013-08-29 16:18:52
【问题描述】:

我在 Scala 中有一个包含一些方法的类,每个方法都有一个执行时间,例如 methodA 需要 10 秒,methodB 需要 5 秒。并且每个方法都异步调用。当我调用methodB 时,它应该取消正在运行另一个方法的线程。我首先调用methodA,2秒后调用methodB。这个问题的最佳解决方案是什么?

def methodA()={
    async{
        // a job that takes 10 seconds
    }
}
def methodB()={
   async{
       // other methods should stop their job
      // a job that takes 5 second
   }
}

def async[T](fn: => Unit): Unit = scala.actors.Actor.actor {
    fn
}

.....

methodA()
methodB()

【问题讨论】:

    标签: scala


    【解决方案1】:

    这是一个想法,基于您的方法正在积极检查它是否仍应运行或取消的假设:

    import concurrent.{ExecutionContext, Future, Promise, blocking, future, promise}
    
    case class Cancelled() extends RuntimeException
    
    object Job {
      def apply[A](fun: (() => Boolean) => A)(implicit ctx: ExecutionContext): Job[A] = 
        new Job[A] {
          private val p = promise[A]
          def result = p.future
          def cancel(): Unit = p.tryFailure(Cancelled())
    
          p tryCompleteWith future {
            fun(() => !p.isCompleted)
          }
        }
    }
    trait Job[A] {
      def result: Future[A]
      def cancel(): Unit
    }
    

    所以Jobcancel() 方法一起体现了未来。您的示例可能与此类似:

    import ExecutionContext.Implicits.global
    
    val a = Job { active =>
      for (i <- 1 to 100 if active()) {
        blocking {
          Thread.sleep(1000)  // doing actual heavy work here
          println(s"A $i")
        }
      }
    }
    
    val b = Job { active =>
      for (i <- 1 to 20 if active()) {
        blocking {
          Thread.sleep(1000)  // doing actual heavy work here
          println(s"B $i")
        }
      }
      println("Goodbye A...")
      a.cancel()
    }
    

    我还看到一个相当 harsh variant 调用 Thread.interrupt

    【讨论】:

    • 很好的解决方案,但是在我的项目中,线程或作业找不到彼此取消。在您的解决方案中b 知道有一个a 工作
    • 嗯,那么您只需要保留一个正在运行的作业的全局列表。在Job.apply 方法中注册它们应该很简单。
    • 好的,但是 apply 方法中的 job 标识符是什么来将其注册到全局地图中?
    猜你喜欢
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    相关资源
    最近更新 更多