Skip to content

Instantly share code, notes, and snippets.

@notyy
Created February 1, 2018 03:50
Show Gist options
  • Save notyy/73a7562f103a6d5a77c097c40cba7a73 to your computer and use it in GitHub Desktop.
Save notyy/73a7562f103a6d5a77c097c40cba7a73 to your computer and use it in GitHub Desktop.
future in different thread
package concurrency.future
import com.typesafe.scalalogging.StrictLogging
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.language.postfixOps
import scala.concurrent.ExecutionContext.Implicits.global
object FutureAwait extends App with StrictLogging {
logger.info("now")
def doSomethingInFuture: Future[Int] = Future {
logger.info("this is in future")
1
}
val rs = Await.result(doSomethingInFuture, 1 second)
logger.info(s"rs is $rs")
}
@chenfengyuan
Copy link

chenfengyuan commented Feb 1, 2018

single thread

import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.language.postfixOps
import scala.concurrent.ExecutionContext

object FutureAwait extends App{
  class CallbackEC extends ExecutionContext {
      override def execute(runnable: Runnable): Unit = {
        runnable.run()
      }

      override def reportFailure(cause: Throwable): Unit = {
        throw new RuntimeException("problem in CallbackEC.reportFailure")
      }
    }
  implicit val ec: CallbackEC = new CallbackEC()
  println(s"now   ${Thread.currentThread().getId}")

  def doSomethingInFuture: Future[Int] = Future {
    println(s"this is in the future   ${Thread.currentThread().getId}")
    1
  }

  println(s"before await   ${Thread.currentThread().getId}")
  val rs = Await.result(doSomethingInFuture, 1 second)
  println(s"after await   ${Thread.currentThread().getId}")
}

@chenfengyuan
Copy link

thread pool

import scala.concurrent.{Await, Future, Promise}
import scala.concurrent.duration._
import scala.language.postfixOps
import scala.concurrent.ExecutionContext
import java.util.concurrent.Executors


object FutureAwait extends App{
  val threadPool = Executors.newFixedThreadPool(1);
  implicit val ec = new ExecutionContext {
    def execute(runnable: Runnable) {
        threadPool.submit(runnable)
    }
    def reportFailure(t: Throwable) {}
  }
  println(s"${Thread.currentThread().getName}\tbefore start")

  def doSomethingInFuture(i: Int): Future[Int] = Future {
    Thread.sleep(100)
    println(s"${Thread.currentThread().getName}\tthis is in the future($i)")
    i
  }
  for {
    i <- 1 to 10
    x <- doSomethingInFuture(i)
  } {
    println(s"${Thread.currentThread().getName}\tthis is in futrue($i) success callback")
  }
  println(s"${Thread.currentThread().getName}\tbefore sleep 10s")
  Thread.sleep(10000)
  println(s"${Thread.currentThread().getName}\tafter sleep 10s")

  println(s"${Thread.currentThread().getName}\tbefore await")
  val rs = Await.result(doSomethingInFuture(100), 1 second)
  println(s"result ${rs}")
  println(s"${Thread.currentThread().getName}\tafter await")
  threadPool.shutdown()
}

@jasonqu
Copy link

jasonqu commented Feb 1, 2018

赞, 可以用 ExecutionContext.fromExecutor 简化一下:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment