synchronized是公平锁码
时间: 2023-12-07 09:21:25 浏览: 223
不完全正确。synchronized是一种可重入锁,它可以保证同一个线程多次获取锁不会发生死锁。而synchronized默认情况下是非公平锁,即当多个线程同时请求锁时,锁将会随机分配给其中一个线程,而不考虑这些线程的请求顺序。这种非公平的锁分配方式可能会导致某些线程长期得不到锁,从而造成线程饥饿问题。但是在Java 5之后,synchronized提供了一种公平锁的实现方式,即在获取锁时按照请求的顺序进行分配,从而避免了线程饥饿问题。可以通过在synchronized关键字后面加上"fair"来创建一个公平锁。
阅读全文
相关推荐


















