线程可以等到其他线程释放锁访问同步块。 我想知道一个线程在其他线程访问synchronized块时可以等待多长时间?什么时候会知道另一个线程释放锁?
答案 0 :(得分:1)
如果已在一个线程中同步变量lock
,则将阻止尝试使用lock
进行同步的所有其他线程,直到lock
不再同步为止。
//all other threads waiting on thread 1...
synchronized(lock)
{
//thread 1 storing digits of pi into linked list... or whatevs.
}
正如其他人所说,线程将无限期地等待一个变量可以自由同步。