Scala入门学习之 【Java线程池】

来源:互联网 发布:反贪知乎 编辑:程序博客网 时间:2024/05/18 02:33






import java.util.concurrent.{Callable, Executor, Executors, Future}object ThreadDemo {  def main(args: Array[String]) {    val pool = Executors.newFixedThreadPool(5)    for (i <- 1 to 10) {      pool.execute(new Runnable {        def run(): Unit = {          println(Thread.currentThread().getName)          Thread.sleep(1000)        }      })    }    //callable 和 Runable都是启动一个线程, 不过Callable可以有返回值///*    val f: Future[Int] = pool.submit(new Callable[Int] {//      override def call(): Int = {//        Thread.sleep(10000)//        100//      }//    })//    var status = f.isDone//    println(s"task status $status")////    Thread.sleep(15000)////    status =  f.isDone//    println(s"task status $status")////    if (status){//      println(f.get())//    }*/  }}


原创粉丝点击