new Thread( public void run() { x = readX } ).start(); ! @volatile var y = _ val yThread = new Thread( public void run() { y = readY } ).start(); yThread.join() xThread.join() z = x+y As GOTO from 60-s
public X run() { readX } ); pool.submit(xTask) ! val yTask = new Task( public void run() { readY } ) pool.submit(yTask) ! z = xTask.get()+yTask.get() X Y Z
= Future{ readY } val z = Future{ … after X and Y } val z = Future{ readX } flatMap { x => readY map ( y=> x+y ) } ! for{ x <- Future{ readY }, y <- Future{ readY }) yield x+y ! Using monadic syntax:
} val y = Future{ readY } await(x) + await(y) } val z = { val s1 = Future{ readX(); } val s2 = Future{ readY(); } val s3 = if (s1.isComplete) if (s2.isComplete) { Promise.success(s1.result + s2.result) } else { s2 map s3 } } else s1 map s3 s3 // Just show the idea, not actual