Mọi người cho em hỏi sự giống và khác nhau giữa async await và multi thread trong C# với ạ?
Khi nào thì mình nên dùng async await, khi nào thì mình nên dùng multi thread ạ?
Em cảm ơn ạ.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Hãy nhìn vào chương trình này nhé:
Và kết quả nó sẽ thế này:
Như vậy có nghĩa là việc async await đang diễn ra trên các thread khác nhau, vậy nên theo quan điểm của anh thì trong C# async / await chẳng quan chỉ là 1 cách viết đơn giản hơn cho lập trình multi thread mà thôi.
Em có thể tham khảo source code của lớp Task sẽ thấy là nó đang sử dụng kết hợp giữa thread pool và task queue.
Đọc source code lớp threadpool anh đang thấy khởi tạo mặc định ban đầu max 32 thread:
đầu tiên e hiểu bất đồng bộ là gì
Là xử lý các tác vụ cùng lúc đúng không ạ?