

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# 코루틴
<a name="coroutines"></a>

는 기본적으로 비동기식 AWS SDK for Kotlin 입니다. SDK for Kotlin은 코루틴에서 호출되는 모든 작업에 `suspend` 함수를 사용합니다.

코루틴에 대한 자세한 가이드는 [공식 Kotlin 설명서를](https://kotlinlang.org/docs/coroutines-overview.html) 참조하세요.

## 동시 요청
<a name="making-concurrent-requests"></a>

[비동기](https://kotlinlang.org/docs/composing-suspending-functions.html#concurrent-using-async) 코루틴 빌더를 사용하여 결과에 신경을 쓰는 동시 요청을 시작할 수 있습니다.는 나중에 결과를 제공하겠다는 약속을 나타내는 가볍고 차단되지 않는 미래를 나타내는 [Deferred](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-deferred/index.html)를 `async` 반환합니다.

결과에 신경쓰지 않는 경우(작업이 완료된 경우에만) [시작](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/launch.html) 코루틴 빌더를 사용할 수 있습니다. `launch`는 개념적으로와 유사합니다`async`. 차이점은 시작이 [작업을](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html) 반환하고 결과 값을 포함하지 않는 반면는를 `async` 반환한다는 것입니다`Deferred`.

다음은 두 키의 콘텐츠 크기를 가져오기 위해 [headObject](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/s3/aws.sdk.kotlin.services.s3/head-object.html) 작업을 사용하여 Amazon S3에 동시 요청하는 예제입니다.

```
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import kotlin.system.measureTimeMillis
import aws.sdk.kotlin.services.s3.S3Client


fun main(): Unit = runBlocking {

    val s3 = S3Client { region = "us-east-2" }
    
    val myBucket = "<your-bucket-name-here>"
    val key1 = "<your-object-key-here>"
    val key2 = "<your-second-object-key-here>"

    val resp1 = async {
        s3.headObject{
            bucket = myBucket
            key = key1
        }
    }

    val resp2 = async {
        s3.headObject{
            bucket = myBucket
            key = key2
        }
    }


    val elapsed = measureTimeMillis {
        val totalContentSize = resp1.await().contentLength + resp2.await().contentLength
        println("content length of $key1 + $key2 = $totalContentSize")
    }

    println("requests completed in $elapsed ms")

}
```

## 차단 요청
<a name="making-clocking-requests"></a>

코루틴을 사용하지 않고 다른 스레딩 모델을 구현하는 기존 코드에서 서비스를 호출하려면 [runBlocking](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html) 코루틴 빌더를 사용할 수 있습니다. 다른 스레딩 모델의 예는 Java의 기존 실행기/미래 접근 방식을 사용하는 것입니다. Java 및 Kotlin 코드 또는 라이브러리를 블렌딩하는 경우이 접근 방식을 사용해야 할 수 있습니다.

이름에서 알 수 있듯이이 `runBlocking`빌더는 새 코루틴을 시작하고 완료될 때까지 현재 스레드를 차단합니다.

**주의**  
 `runBlocking`는 일반적으로 코루틴에서 사용해서는 안 됩니다. 일반 차단 코드를 일시 중지 스타일(예: 기본 함수 및 테스트)로 작성된 라이브러리에 연결하도록 설계되었습니다.