简体   繁体   English

两个java线程完全相同毫秒

[英]Two java threads at exactly same milli second

If two threads try to access the synchronized method at the VERY exact same time with exact milli-second or (whatever minimum CPU unit is).. What will happen? 如果两个线程试图在完全相同的时间以精确的毫秒或(无论最小CPU单位是什么)访问同步方法。会发生什么? How JVM/System will resolve this issue if they have same priority as well. 如果JVM / System具有相同的优先级,它将如何解决此问题。 Will it create deadlock? 它会造成僵局吗? Also, where this information about locks is stored? 此外,存储有关锁的信息?

That's the whole point of threading. 这是线程的全部要点。 The operating system's job scheduler will allow one to go before the other. 操作系统的作业调度程序将允许一个在另一个之前。 No deadlock will occur. 不会发生死锁。

Actually, the job scheduler is for heavyweight forking of processes. 实际上,作业调度程序用于重量级的进程分叉。 The JVM handles which Thread s have a turn to execute or enter a synchronized block. JVM处理哪些Thread转向执行或进入同步块。

Entering synchronized block is translated internally to obtaining some atomic mutex or semaphore in the operating system. 在内部转换输入synchronized块以在操作系统中获得一些原子互斥或信号量。 These constructs are guaranteed to be atomic. 这些结构保证是原子的。 Thus nothing wrong will happen, one method is always first . 因此,没有任何错误会发生,一种方法总是第一位的 Even in a multi-core, multi-CPU machine only one thread can obtain semaphore/mutex at once. 即使在多核,多CPU机器中,也只有一个线程可以同时获得信号量/互斥量。

If will definitely not create a deadlock. 如果肯定不会造成僵局。 To create a deadlock you need two resources to compete. 要创建死锁,您需要两个资源来竞争。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM