Scalaz(45)- concurrency :Task-函数式多线程编程核心配件

    我们在上一节讨论了scalaz Future,我们说它是一个不完善的类型,最起码没有完整的异常处理机制,只能用在构建类库之类的内部环境。如果scalaz在Future类定义中增加异常处理工具的话,用户就会经常遇到Future[Throwable\/A]这样的类型,那么在进行Monadic编程时就必须使用Monad Transformer来匹配类型,程序就会变得不必要的复杂。scalaz的解决方案就是把Future[Throwable\/A]包嵌在Task类里,然后把所有Future都统一升格成Task。Task是个Monad, 这样,我们就可以统一方便地用Task来进行多线程函数式编程了。我们先看看Task的定义:scalaz.concurrent/Task.scala

class Task[+A](val get: Future[Throwable \/ A]) {

  def flatMap[B](f: A => Task[B]): Task[B] =
    new Task(get flatMap {
      case -\/(e) => Future.now(-\/(e))
      case \/-(a) => Task.Try(f(a)) match {
        case e @ -\/(_) => Future.now(e)
        case \/-(task) => task.get
      }
    })

  def map[B](f: A => B): Task[B] =
    new Task(get map { _ flatMap {a => Task.Try(f(a))} })
...

Task实现了flatMap,所以是个Monad,我们可以在for-comprehension中使用Task。

Task的构建方式与Future一样:

1 val tnow = Task.now { println("run now ..."); 3+4 }
2                                      //> run now ...
3                                      //| tnow  : scalaz.concurrent.Task[Int] = scalaz.concurrent.Task@1a968a59
4 val tdelay = Task.delay { println("run delay ...");  3+4 }
5                                     //> tdelay  : scalaz.concurrent.Task[Int] = scalaz.concurrent.Task@13deb50e
6 val tapply = Task { println("run apply ..."); 3+4 }
7                                    //> tapply  : scalaz.concurrent.Task[Int] = scalaz.concurrent.Task@59494225

同样,now函数是即时运算的。它就是一个lifter,能把一个普通运算直接升格为Task。

针对Task有几种运算方法:

 1 tnow.unsafePerformSync                            //> res0: Int = 7
 2 tdelay.unsafePerformSync                          //> run delay ...
 3                                                   //| res1: Int = 7
 4 tnow.unsafePerformAsync {
 5   case \/-(a) => println(s"the result is: $a")
 6   case -\/(e) => println(e.getMessage)
 7 }                                                 //> the result is: 7
 8 tdelay.unsafePerformAsync {
 9   case \/-(a) => println(s"the result is: $a")
10   case -\/(e) => println(e.getMessage)
11 }                                                 //> run delay ...
12                                                   //| the result is: 7
13 tapply.unsafePerformAsync {
14   case \/-(a) => println(s"the result is: $a")
15   case -\/(e) => println(e.getMessage)
16 }
17 Thread.sleep(1000)                                //> run apply ...
18                                                   //| the result is: 7

从上面的例子我们可以得出:tnow已经完成了运算,因为运算结果没有"run now ..."提示了。tdelay和tapply都是存在trampoline结构里的。但tapply存在更深一层的结构里,所以我们必须拖时间来等待tapply的运算结果。tdelay存放在Future.Suspend结构里,而tapply是存放在Future.Async结构里的,所以tdelay是一种延迟运算,而tapply就是异步运算了:

 1 def delay[A](a: => A): Task[A] = suspend(now(a))
 2 def suspend[A](a: => Task[A]): Task[A] = new Task(Future.suspend(
 3     Try(a.get) match {
 4       case -\/(e) => Future.now(-\/(e))
 5       case \/-(f) => f
 6   }))
 7 //Future.suspend:
 8  def suspend[A](f: => Future[A]): Future[A] = Suspend(() => f)
 9 
10  def apply[A](a: => A)(implicit pool: ExecutorService = Strategy.DefaultExecutorService): Task[A] =
11     new Task(Future(Try(a))(pool))
12 //Future.apply
13 def apply[A](a: => A)(implicit pool: ExecutorService = Strategy.DefaultExecutorService): Future[A] = Async { cb =>
14     pool.submit { new Callable[Unit] { def call = cb(a).run }}
15   }

好了,我们再看看Task是怎样处理异常情况的:

 1 def eval(value: => Int) = Task { Thread.sleep(1000); value }
 2                                                   //> eval: (value: => Int)scalaz.concurrent.Task[Int]
 3 eval( 3 * 7 ).onFinish {
 4   case None => Task { println("finished calculation successfully.") }
 5   case Some(e) => Task { println(s"caught error [${e.getMessage}]") }
 6 }.unsafePerformSyncAttempt match {
 7   case -\/(e) => println(s"calculation error [${e.getMessage}]")
 8   case \/-(a) => println(s"the result is: $a")
 9 }                                                 //> finished calculation successfully.
10                                                   //| the result is: 21
11 // 异常处理
12 eval( 3 * 7 / 0 ).onFinish {
13   case None => Task { println("finished calculation successfully.") }
14   case Some(e) => Task { println(s"caught error [${e.getMessage}]") }
15 }.unsafePerformAsync {
16   case -\/(e) => println(s"calculation error [${e.getMessage}]")
17   case \/-(a) => println(s"the result is: $a")
18 }
19 Thread.sleep(2000)                                //> caught error [/ by zero]
20                                                   //| calculation error [/ by zero]

精准异常处理例子:

1 import java.util.concurrent._
2 val timedTask = Task {Thread.sleep(2000); 3+4}   
3                      //> timedTask  : scalaz.concurrent.Task[Int] = scalaz.concurrent.Task@3d921e20
4 timedTask.timed(1 second).handleWith {
5   case e: TimeoutException => Task { println(s"calculation exceeding time limit: ${e.getMessage}") }
6 }.unsafePerformSync           //> calculation exceeding time limit: Timed out after 1000 milliseconds
7                               //| res2: AnyVal{def getClass(): Class[_ >: Int with Unit <: AnyVal]} = ()

再看一些多线程编程例子:

 1 val tasks = (1 |-> 5).map(n => Task{ Thread.sleep(100); n })
 2                                 //> tasks  : List[scalaz.concurrent.Task[Int]] = List(scalaz.concurrent.Task@61
 3                                 //| 8b19ad, scalaz.concurrent.Task@2d3379b4, scalaz.concurrent.Task@30c15d8b, s
 4                                 //| calaz.concurrent.Task@5e0e82ae, scalaz.concurrent.Task@6771beb3)
 5 //并行运算list of tasks
 6 Task.gatherUnordered(tasks).unsafePerformSync     //> res3: List[Int] = List(1, 2, 3, 4, 5)
 7 val sb = new StringBuffer                         //> sb  : StringBuffer = 
 8 val t1 = Task.fork { Thread.sleep(100); sb.append("a"); Task.now("a")}
 9                                  //> t1  : scalaz.concurrent.Task[String] = scalaz.concurrent.Task@6200f9cb
10 val t2 = Task.fork { Thread.sleep(800); sb.append("b"); Task.now("b")}
11                                  //> t2  : scalaz.concurrent.Task[String] = scalaz.concurrent.Task@2002fc1d
12 val t3 = Task.fork { Thread.sleep(200); sb.append("c"); Task.now("c")}
13                                  //> t3  : scalaz.concurrent.Task[String] = scalaz.concurrent.Task@69453e37
14 val t4 = Task.fork { Thread.sleep(100); sb.append("d"); Task.now("d")}
15                                  //> t4  : scalaz.concurrent.Task[String] = scalaz.concurrent.Task@6f4a47c7
16 val t5 = Task.fork { Thread.sleep(400); sb.append("e"); Task.now("e")}
17                                  //> t5  : scalaz.concurrent.Task[String] = scalaz.concurrent.Task@ae13544
18 val t6 = Task.fork { Thread.sleep(100); sb.append("f"); Task.now("f")}
19                                  //> t6  : scalaz.concurrent.Task[String] = scalaz.concurrent.Task@3d34d211
20 val r = Nondeterminism[Task].nmap6(t1,t2,t3,t4,t5,t6)(List(_,_,_,_,_,_))
21                                  //> r  : scalaz.concurrent.Task[List[String]] = scalaz.concurrent.Task@394df057 
22 r.unsafePerformSync              //> res4: List[String] = List(a, b, c, d, e, f)

看个耗时算法的并行运算吧:

 1 def seqFib(n: Int): Task[Int] =  n match {
 2   case 0 | 1 => Task now  1
 3   case n => {
 4     for {
 5       x <- seqFib(n-1)
 6       y <- seqFib(n-2)
 7     } yield x + y
 8   }
 9  }                                                //> seqFib: (n: Int)scalaz.concurrent.Task[Int]
10  //并行算法
11  def parFib(n: Int): Task[Int] = n match {
12     case 0 | 1 => Task now 1
13     case n => {
14        val ND = Nondeterminism[Task]
15        for {
16          pair <- ND.both(parFib(n-1), parFib(n-2))
17          (x,y) = pair
18        } yield x + y
19     }
20  }                                                //> parFib: (n: Int)scalaz.concurrent.Task[Int]
21  def msFib(n: Int, fib: Int => Task[Int]) = for {
22    b <- Task now { System.currentTimeMillis() }
23    a <- fib(n)
24    e <- Task now { System.currentTimeMillis() }
25  } yield (a, (e-b))                               //> msFib: (n: Int, fib: Int => scalaz.concurrent.Task[Int])scalaz.concurrent.T
26                                                   //| ask[(Int, Long)]
27  
28  msFib(20, parFib).unsafePerformSync              //> res3: (Int, Long) = (10946,373)
29  msFib(20, seqFib).unsafePerformSync              //> res4: (Int, Long) = (10946,17)

哎呀!奇怪了,为什么并行算法要慢很多呢?这个问题暂且放一放,以后再研究。当然,如果有读者能给出个解释就太感激了。 Task的线程池是如何分配的呢?看看Task.apply和Task.fork:

 /** Create a `Task` that will evaluate `a` using the given `ExecutorService`. */
  def apply[A](a: => A)(implicit pool: ExecutorService = Strategy.DefaultExecutorService): Task[A] =
    new Task(Future(Try(a))(pool))
def fork[A](a: => Task[A])(implicit pool: ExecutorService = Strategy.DefaultExecutorService): Task[A] =
    apply(a).join
//Future.apply
/** Create a `Future` that will evaluate `a` using the given `ExecutorService`. */
  def apply[A](a: => A)(implicit pool: ExecutorService = Strategy.DefaultExecutorService): Future[A] = Async { cb =>
    pool.submit { new Callable[Unit] { def call = cb(a).run }}

这两个函数都包括了一个隐式参数implicit pool: ExecutorService。默认值是Strategy.DefultExecutorService。我们可以这样指定线程池:

1  Task {longProcess}(myExecutorService)
2  Task.fork { Task {longProcess} }(myExecutorService)

下面是一个动态指定线程池的例子:

 1 import java.util.concurrent.{ExecutorService,Executors}
 2 type Delegated[A] = Kleisli[Task,ExecutorService,A]
 3 def delegate: Delegated[ExecutorService] = Kleisli(e => Task.now(e))
 4                             //> delegate: => demo.ws.task.Delegated[java.util.concurrent.ExecutorService]
 5 implicit def delegateTaskToPool[A](ta: Task[A]): Delegated[A] = Kleisli(x => ta)
 6                             //> delegateTaskToPool: [A](ta: scalaz.concurrent.Task[A])demo.ws.task.Delegated[A]
 7 val tPrg = for {
 8   p <- delegate
 9   b <- Task("x")(p)
10   c <- Task("y")(p)
11 } yield c                   //> tPrg  : scalaz.Kleisli[scalaz.concurrent.Task,java.util.concurrent.Executor
12                             //| Service,String] = Kleisli(<function1>)
13 tPrg.run(Executors.newFixedThreadPool(3)).unsafePerformSync
14                             //> res3: String = y

当然,Task和scala Future之间是可以相互转换的:

 1 import scala.concurrent.{Future => sFuture}
 2 import scala.util.{Success,Failure}
 3 import scala.concurrent.ExecutionContext
 4 def futureToTask[A](fut: sFuture[A])(implicit ec: ExecutionContext): Task[A] =
 5   Task.async {
 6     cb =>
 7       fut.onComplete {
 8         case Success(a) => cb(a.right)
 9         case Failure(e) => cb(e.left)
10       }
11   }
12 def taskToFuture[A](ta: Task[A]): sFuture[A] = {
13   val prom = scala.concurrent.Promise[A]
14   ta.unsafePerformAsync {
15     case -\/(e) => prom.failure(e)
16     case \/-(a) => prom.success(a)
17   }
18   prom.future
19 }

与Future不同的是:Task增加了异常处理机制。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏栗霖积跬步之旅

1.2使用多线程

一个进程正在运行时,至少会有一个线程在运行。线程在后台默默执行,比如调用main方法的线程就是如此,它是由JVM创建的。 class Test { pu...

1846
来自专栏java一日一条

java中的异常总结

一般面试中java Exception(runtimeException )是必会被问到的问题

863
来自专栏进击的君君的前端之路

this_原型链_继承

972
来自专栏好好学java的技术栈

java并发基础篇(五): 创建线程的四种方式

在详细了解这四种方法之前,先来理解一下为什么线程要这样创建:形象点来说,Thread是一个工人,run()方法里面的便是他的任务栏,这个任务栏默认是空的。当你想...

401
来自专栏desperate633

深入理解Java的四种引用类型强引用(StrongReference)软引用(SoftReference)弱引用(WeakReference)虚引用(PhantomReference)多引用类型的可达

Java垃圾回收机制可以用 3 个词来概括: where, when 和 how?

441
来自专栏微信公众号:Java团长

Java基础知识详细总结

ClassLoader使用的是双亲委托模型来搜索类的,每个ClassLoader实例都有一个父类加载器的引用(不是继承的关系,是一个包含的关系),虚拟机内置的类...

913
来自专栏Android干货园

Kotlin中级(5)- - - 初识Kotlin中的对象.md

主构造和次构造可以并存么?当然可以。但是如果类有一个主构造函数,那么每个次构造函数都需要委托给主构造函数。也就是说,次构造函数在最后还是要用到主构造函数。

612
来自专栏Java帮帮-微信公众号-技术文章全总结

Java面试系列2

六、&和&&的区别? &是位运算符,表示按位与运算,&&是逻辑运算符,表示逻辑与(and) 七、swtich是否能作用在byte上,是否能作用在long上,是否...

2756
来自专栏软件开发

JavaSE学习总结(八)—— 异常处理(Exception)

一、理解异常及异常处理的概念 异常就是在程序的运行过程中所发生的不正常的事件,它会中断正在运行的程序。 异常不是错误 程序中关键的位置有异常处理,提高程序的稳定...

1999
来自专栏LinkedBear的个人空间

唠唠SE的集合-10——Collections工具类

461

扫码关注云+社区