简体   繁体   中英

where can I find java thread safe objects concept description?

I heard a lot of times that one of modern concepts of java apps structure creation is effectively thread-safe objects concept. This means that we only use final entity classes in our app. Where can I find comprehensive guide of that concept? How can I apply this concept to ordinary MVC web app, based on hibernate and spring, using DAO and DTO approach?

Your question is really wide.

You can start from https://en.wikipedia.org/wiki/Thread_safety and it will be only a start of long journey that will take you through http://www.cs.umd.edu/~pugh/java/memoryModel/ to internals of java and later down to internals of modern CPUs.

Behind this things lies following things:

  • state - some value is equal to some "X", for example name John
  • changeable state - some value was John at some moment and now it should be Donald
  • execution flow - sequence of program steps being executed one-by-one
  • thread - executor of the flow
  • multi-thread - you code has several flows executed in parallel

(for example one web query requests GET person by it's id=8374 and other requests GET all persons to fetch all known users)

What can go wrong - one call returns that user is Donald and list off all users returns that he is John (stale value is observed from other thread).

Same issues can happen at "lower level" - for example one thread changes counter = counter + 1, and other thread is doing the same. If everything OK and starting value of counter = 3, the 1st one will take 3, add 1, and set counter = 4. Then 2nd one will come, will take 4, add 1, and set counter to 5.

If things will go wrong you can have situation that 1st will take value of counter as 3, take value into memory and will pause for internal issues for a moment. In that moment 2nd will observe that counter is 3 and will take it into it's memory, will do it's math and will store counter = 4. The first one will resume execution, will do 3 + 1 (without re-reading value from counter) and will do store 4 to counter. As result - you "lost" one of increments.

Same issues exists at web level, at java level, at CPU level.

Main directions to work around this issues ar:

"Immutability" - concept that value is assigned once and never changes after that - it is can be tried to be achieved by usage of final on variables.

"Synchronization" - you can mark some part of code using synchronize keyword to be executed only as one atomic operation without interleaving by other threads (the 2nd thread will wait till 1st has finished all steps).

Etc. It is really big area of computer science. Please go to the wiki pages mentioned earlier.

In case of spring / hibernate - you can't make everything immutable, so you will create code that mutates state to be execute in single thread using synchronize blocks. Might be in java code, might be in db code.

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