Java analysis thread is not safe

Thread unsafe

Now there is a text document, shared on the network, A, B, C, D (4 threads) can access the file, and operate the file at the same time, all add content to the file, and then they all save it, who saved it last? The document shows whose content is displayed, and the content added by others is lost.

Thread safe

Now A is opening the file and appending content. At this time, B wants to open the file, and it will prompt that the user is operating the file. Please wait a moment. After A finishes modifying the file and saving it, then B can open the file. People also modify the same.

Thread synchronization is a queuing problem. Also called thread safety.

Guess you like

Origin blog.csdn.net/weixin_43088443/article/details/112971233
Recommended