其他线程访问同步块时线程等待多长时间?

时间:2013-08-28 17:24:05

标签: java multithreading synchronized thread-synchronization

线程可以等到其他线程释放锁访问同步块。 我想知道一个线程在其他线程访问synchronized块时可以等待多长时间?什么时候会知道另一个线程释放锁?

1 个答案:

答案 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.
}

正如其他人所说,线程将无限期地等待一个变量可以自由同步。