Multithreading Scala-多线程,在任何子线程完成时完成主线程
我正在构建一个方法,它接受x大小的方法序列,并返回第一个要完成的方法的结果Multithreading Scala-多线程,在任何子线程完成时完成主线程,multithreading,scala,Multithreading,Scala,我正在构建一个方法,它接受x大小的方法序列,并返回第一个要完成的方法的结果 def invokeAny(work: Seq[() => Int]): Int = ??? 如何通过使用线程来实现这一点?(不允许期货交易) 这是我能想到的最好的方法,但似乎不是在所有情况下都有效 def invokeAny(work: Seq[() => Int]): Int = { @volatile var result = 0 // set to return value of any w
def invokeAny(work: Seq[() => Int]): Int = ???
如何通过使用线程来实现这一点?(不允许期货交易)
这是我能想到的最好的方法,但似乎不是在所有情况下都有效
def invokeAny(work: Seq[() => Int]): Int = {
@volatile var result = 0 // set to return value of any work function
val main = Thread.currentThread()
val threads: Seq[Thread] = work.map(work => new Thread( new Runnable {
def run { result = work(); main.interrupt(); }}))
threads.foreach(_.start())
for(thread <- threads) {
try {
thread.join()
} catch {
// We've been interrupted: finish
case e: InterruptedException => return result
}
}
return result
}
def invokeAny(工作:Seq[()=>Int]):Int={
@volatile var result=0//设置为任何工作函数的返回值
val main=Thread.currentThread()
val threads:Seq[Thread]=work.map(work=>newthreads(newrunnable{
def run{result=work();main.interrupt();}}})
threads.foreach(u.start())
(线程不是最漂亮的答案,但似乎有效:
def invokeAny(work: Seq[() => Int]): Int = {
@volatile var result = 0 // set to return value of any work function
val main = Thread.currentThread()
var threads: Seq[Thread] = Seq()
//Interrupts all threads after one is interrupted
def interruptAll = {
main.interrupt()
for(thread <- threads) {
thread.interrupt()
}
}
threads = work.map(work => new Thread(
new Runnable {
def run {
result = try {
work() } catch {
case e:InterruptedException => return
}
interruptAll;
}
}))
threads.foreach(_.start())
for(thread <- threads) {
try {
thread.join()
} catch {
// We've been interrupted: finish
case e: InterruptedException => return result
}
}
return result
}
def invokeAny(工作:Seq[()=>Int]):Int={
@volatile var result=0//设置为任何工作函数的返回值
val main=Thread.currentThread()
变量线程:Seq[Thread]=Seq()
//在一个线程被中断后中断所有线程
def interruptAll={
main.interrupt()
对于(新线程(
新的可运行{
def运行{
结果=尝试{
work()}catch{
案例e:InterruptedException=>返回
}
打断一切;
}
}))
threads.foreach(u.start())
对于(thread使用BlockingQueue,无共享可变状态,工作线程写入队列,主线程等待完成并从队列读取,然后对结果执行sum之类的操作
def invokeAny1(work: Seq[() => Int]): Int = {
val queue = new ArrayBlockingQueue[Int](work.size)
val threads: Seq[Thread] = work.map(w => new Thread( new Runnable {
def run {
val result= w()
queue.put(result) }}))
threads.foreach(_.start())
threads.foreach(_.join())
var sum:Int=0
while(!queue.isEmpty) {
sum +=queue.take()
}
sum
}
使用倒计时锁存器。
工作线程增加一个原子变量。
当所有线程都完成时,闩锁被释放,主线程可以从原子变量读取数据
def invokeAny2(work: Seq[() => Int]): Int = {
val total=new AtomicInteger
val latch= new CountDownLatch(work.size)
val threads: Seq[Thread] = work.map(w => new Thread( new Runnable {
def run {
val result= w()
total.getAndAdd(result)
latch.countDown
}}))
threads.foreach(_.start())
latch.await //wait till the latch is released
total.get
}
}
我建议使用Futures而不是threadsYeah我知道,但我想知道如何使用threads使用java.util.concurrent.CountDownLatch
,值为1。您的子线程将调用latch.countDown()
,主线程将使用latch.await()等待它
我测试了您的代码,但它似乎给出了错误的答案,并且比我测试的代码花费了四倍的时间posted://My 代码与您发布的稍有不同,它将等待所有线程完成。如果您只希望第一个线程的结果完成,请在第二个代码段中使val-lack=new CountDownLatch(1)