ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • airframe-control: Parallel
    scala/airframe 2023. 7. 6. 16:39

    airframe-control 의 Parallel 을 테스트 해 본다.

     

    코드

    import wvlet.airframe.control.Parallel
    
    import java.text.SimpleDateFormat
    import java.util.Date
    
    object AirframeParallel {
      def now(): String = {
        val pattern = "HH:mm:ss.SSSZ"
        val simpleDateFormat = new SimpleDateFormat(pattern)
        simpleDateFormat.format(new Date())
      }
    
      def main(args: Array[String]): Unit = {
        val source: Seq[Int] = Seq(1, 2, 3, 4, 5)
    
        println(s"START: ${now()}")
        val results: Seq[Int] = Parallel.run(source, parallelism = 2) { i =>
          println(s"${now()}: starts [$i]")
          Thread.sleep(i * 1000L)
          println(s"${now()}: ends [$i]")
          i * 1000
        }
        println(s"RESULT: $results")
        println(s"FINISH: ${now()}")
      }
    }

     

    결과

    START: 16:33:10.527+0900
    16:33:10.923+0900: starts [2]
    16:33:10.923+0900: starts [1]
    16:33:11.924+0900: ends [1]
    16:33:11.925+0900: starts [3]
    16:33:12.929+0900: ends [2]
    16:33:12.929+0900: starts [4]
    16:33:14.939+0900: ends [3]
    16:33:14.939+0900: starts [5]
    16:33:16.934+0900: ends [4]
    16:33:19.952+0900: ends [5]
    RESULT: List(1000, 2000, 3000, 4000, 5000)
    FINISH: 16:33:20.001+0900

     

    참고

    Parallel.iterate() method 도 있다.

    `Iterator` can be used instead of `Seq` as a source. This version is useful to handle a very large data.

     

     

    개인 의견

    scala future 를 사용했다면 보통 implicit ExecutionContext 를 제공해야 하는데, 코드상에서 전혀 보이지 않아 구현 코드를 살펴봤다. java 의 ThreadPool 을 사용했는데, Parallel.run() 을 수행할 때 마다 pool 을 생성하고 종료하고 있다. 자주 수행된다면 성능상 이슈도 있을지도?

    'scala > airframe' 카테고리의 다른 글

    airframe-control: Retry  (0) 2023.07.06
    airframe-di: Dependency Injection  (0) 2022.11.19

    댓글

Designed by Tistory.