Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我们创建例如 3 个线程 T1、T2 和 T3,它们每个都调用相同的函数或过程,那么如果我们调用它们一段时间T1.start() T2.start() T3.start(),它会工作吗?或者我们需要同步它们??
T1.start() T2.start() T3.start()
您不同步功能,而是同步它们对资源的访问。
如果任何函数修改了公共资源*而其他函数需要访问它,无论是读取还是写入,您都需要同步对资源的访问。否则,无需添加同步。
仅当您在不同线程中读取/写入相同的变量(状态)时才需要同步。如果方法仅使用局部变量 - 您不需要同步它们。