티스토리 뷰

공부

[kotlin] semaphore & coroutine

승가비 2023. 2. 26. 11:51
728x90
val semaphore = Semaphore(5)

coroutineScope {
    list.map {

        async {
            semaphore.acquire()
            
            // logic(it)
            
            semaphore.release()
        }
    }.awaitAll()
}

https://stackoverflow.com/questions/55877419/how-to-launch-10-coroutines-in-for-loop-and-wait-until-all-of-them-finish/75569716#75569716

 

how to launch 10 coroutines in for loop and wait until all of them finish?

I need to fill list of objects from DB. And before passing value to itemes I want all of them to finish. Is here any short way calling await() for each item to wait. I want to make clean code, May be

stackoverflow.com

https://stackoverflow.com/questions/58428584/limiting-the-maximum-number-of-coroutines-that-can-run-in-a-scope/75569724#75569724

 

Limiting the maximum number of coroutines that can run in a scope

I am translating our current application from Java to Kotlin and I came upon this problem. The java implementation used to use threads to transfer data from the server. It would create about 100

stackoverflow.com

https://origogi.github.io/coroutine/%EC%BD%94%EB%A3%A8%ED%8B%B4-%EC%8A%A4%EC%BD%94%ED%94%84/

 

[Coroutine] 코루틴 스코프

 

origogi.github.io

https://devroach.tistory.com/139

 

CoroutineScope 과 Runblocking 의 차이

CoroutineScope VS Runblocking Kotlin Coroutines 을 학습했다면 위와 같은 고민을 하고 있을 가능성이 높다고 생각한다. 도대체 둘의 차이는 무엇일까? 일단 아래 예시 코드를 한번 보자. // 1번 코드 fun main()

devroach.tistory.com

 

728x90
댓글