Unity/기타
쓰레드 풀 & async ask 사용하기
소나무꼴
2019. 9. 17. 17:53
int test_i;
int counter = 400;
void Start()
{
for(int i =0; i<10; i++)
{
var taskSource = new TaskCompletionSource();
ThreadPool.QueueUserWorkItem(o => taskSource.SetResult(Test4Async().Result));
// 이 주석이 없다면 쓰레드들은 동시에 발동하지 않고 순차적으로 발동한다
// var result = await taskSource.Task;
}
}
async Task Test4Async()
{
Debug.LogFormat("Test4Async Start {0}", Thread.CurrentThread.ManagedThreadId);
test_i++;
for (int i = 0; i < counter; i++)
for (int j = 0; j < counter; j++) ;
Debug.LogFormat("Delay");
await Task.Delay(1);
Debug.LogFormat("Test4Async End {0}", Thread.CurrentThread.ManagedThreadId);
return test_i;
}
갤럭시 4s 에서 테스트시
Test4Async 1번 호출시 10~12초.
Test4Async 10를 순차적으로 호출 : 80~100초
Test4Async 10개를 쓰레드로 호출 : 25~25초