CPU 单核:需要易失性还是​​同步?

发布于 2025-01-03 11:09:14 字数 201 浏览 3 评论 0原文

在Java中,Volatile和synchronized使得多线程可以协同工作并相互理解。但是,如果您在单核系统上使用多线程,则意味着不是真正的多线程工作,操作系统只是从一个线程更改为另一个线程。 (因此,一个变量只在同一时刻使用一个)

因此,我认为没有必要使用安全线程技术,例如 volatile 或 synchronized 。 是真的吗?

谢谢 :)

In Java, Volatile and synchronized make multithreads can work together and understand each others as well. But, if you use multithread on single-core system, it mean not really multithread work, OS just changes from this thread to thread. (So, one variable just use ONE at exactly moment)

So, I think it not necessary to use safe-thread technique such as volatile or synchronized.
Does it true ?

thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独守阴晴ぅ圆缺 2025-01-10 11:09:14

你错了。同步在单核系统上仍然是一个问题,因为 JRE 可能在任何时候切换线程(包括线程位于关键部分时)。

You are incorrect. Synchronization is still an issue on a single-core system, as the JRE may switch threads at any point (including while a thread is in a critical section).

維他命╮ 2025-01-10 11:09:14

同步与核心数量无关。它基于有多少线程竞争共享资源。当多个线程竞争时,您需要同步。

Synchronization is independent of how many cores. It is based on how many threads competing for shared resource. When more than one thread competing you need Synchronization.

淡墨 2025-01-10 11:09:14

无论是单核还是多核系统,同步都很重要。为了防止死锁,你需要正确使用同步。除了预防死锁之外,同步在GUI应用程序中也很重要,因为我们需要在后台计算密集的东西并定期更新图形用户界面。

Synchronisation is important regardless of Single core or Multi core systems.In order to prevent Deadlocks,you need to correctly use Synchronisation.and besides Deadlock Prevention,Synchronisation is also essential in GUI applications where we need to compute intensive things in the background and periodically update GUI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文