你说的是同步的作用,没有错。但是请注意str是内部变量, 如果它是全局变量,并且被每个线程所共用的话,它的锁定才真正有效。参考: Every Java object has an associated lock. Java locks can be held by no more than one thread at a time. When a thread enters a synchronized block of code, the thread blocks and waits until the lock is available, acquires the lock when it becomes available, and then executes the block of code.
如果它是全局变量,并且被每个线程所共用的话,它的锁定才真正有效。参考:
Every Java object has an associated lock. Java locks can be held by no more than one thread at a time. When a thread enters a synchronized block of code, the thread blocks and waits until the lock is available, acquires the lock when it becomes available, and then executes the block of code.
:)