1

A Java thread A fails to execute synchronized statement as another thread has got the monitor.
The thread A is queued by the JVM?
And how thread A is activated after, via 1) or 2) ?

  1. As soon as the monitor is released JVM will send up this signal , thread A may be activated
  2. the JVM will detect whether the monitor is available in a period of time, if the monitor is available, thread A may be activated
6
  • What happens: the thread is blocked, and will stop and not proceed. It's a bit like queueing but depends on the OS and the thread library used.. #2 is closest; when a thread is unblocked, it's put in the ready queue (ready = able to run) but may not run immediately if the OS has other threads/processes that are running and still able to proceed. Commented Dec 15, 2018 at 3:47
  • One addendum: last I checked, the JVM could optimize a synchronized block that seldom was contested (seldom blocked) and turn the synchronized check into a spin-lock. These don't queue, they just burn CPU cycles until the lock is released. Commented Dec 15, 2018 at 3:51
  • "fails to execute" You may want to qualify what you mean with 'failed', because it doesn't fail, it just waits until it can proceed. Commented Dec 15, 2018 at 9:08
  • yes,perfect answer @markspace I mainly am confused whether the blocked thread which is because of 'synchronized' sentence is informed by the monior release action like the blocked thread which is because of 'wait' sentence is informed by notify sentence Commented Dec 19, 2018 at 8:47
  • What I mean for 'fails to execute' is it is blocked until can obtain the monitor @MarkRotteveel Commented Dec 19, 2018 at 8:52

1 Answer 1

1

The Java Language Specification says it in section 17.1 Sysnchronization:

[...] Only one thread at a time may hold a lock on a monitor. Any other threads attempting to lock that monitor are blocked until they can obtain a lock on that monitor. [...]

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.