当前位置:网站首页>Thinking: what is asynchrony and thread safety

Thinking: what is asynchrony and thread safety

2022-06-12 14:36:00 Understand the principle + good code skills

1) Asynchronously :

1. In a thread 1 Initiate a query request

2. Blocking threads 2 Wait for the result

3. Threads 2 Get the results , And put the results push To thread 1 Of the queue

...

If so : Threads 1 Launched 100 Requests , It can be seen that , What must be received is also returned in sequence .

2) Rethink synchronization :

Threads 1 launch func call, And get the result directly .

If so : Threads 1 Launched 100 Requests , It can be seen that , What must be received is also returned in sequence .

summary : So it seems , For the processing of a data , In fact, although it is asynchronous , But from the final call result , It's no different from a function call in a thread ! therefore , Do this ( Let a thread maintain the reading and writing of data all the time ), So it must be thread safe .

原网站

版权声明
本文为[Understand the principle + good code skills]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010504207356.html