注意,该语句并不意味着任何正确的单线程程序也是正确的多线程程序,作者只是说明有可能将任何单线程程序转换为在多线程环境中执行.
但是,如果程序在单线程环境中已经不正确,那么在更复杂的多线程环境中它可能不正确.
从书中:
Since any single threaded program is also a valid multithreaded
program, it cannot be thread safe if it is not even correct in a
single threaded environment.
If an object is correctly implemented, no sequence of operations-calls
to public methods and reads or writes of public fields should be able
to violate any of its invariants or post conditions. No set of
operations performed sequentially or concurrently on instances of a
thread safe class can cause an instance to be in an invalid state.
还有这个:
If the loose use of “correctness” here bothers you, you may prefer to
think of a thread safe class as one that is no more broken in a
concurrent environment than in a single threaded environment.
@yshavit在开始考虑多线程正确性之前,请确保至少具有单线程正确性.