简体   繁体   中英

SCJD synchronized statement vs Lock

I'm trying to justify the use of synchronized statement vs Java Concurrent API for my SCJD.

So far the only reason I can give is it is more simple, easier to understand and cleaner.

Are there any better technical reasons?

The best reason is robustness.

A lock must be released, and forgetting to do it, or not doing it properly (in a finally block) can have disastrous effects.

Since synchronized protects a whole block of code or a method, and automatically releases the lock when the block or method ends, using it is safer and leads to less programming errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM