Waiting For A Synchronized Method

C

cppaddict

Say thread1 begins executing a synchronized method.

While it's executing, both thread2 and thread3 call the synchronized
method, in that order.

When thread1 ends executing and gives up its lock, is thread2
guaranteed to execute next (because it's been waiting longer than
thread3), or is it possible that thread3 executes first?

My experiments seem to point to thread2 always running first in the
above situation, but I'd like to verify this.

Thanks,
John
 
C

Chris Uppal

cppaddict said:
Say thread1 begins executing a synchronized method.

While it's executing, both thread2 and thread3 call the synchronized
method, in that order.

When thread1 ends executing and gives up its lock, is thread2
guaranteed to execute next
No.

or is it possible that thread3 executes first?

Yes. Or rather, it is implementation-dependent, so don't assume anything.

-- chris
 
F

Frank

Say thread1 begins executing a synchronized method.

While it's executing, both thread2 and thread3 call the synchronized
method, in that order.

When thread1 ends executing and gives up its lock, is thread2
guaranteed to execute next (because it's been waiting longer than
thread3), or is it possible that thread3 executes first?

My experiments seem to point to thread2 always running first in the
above situation, but I'd like to verify this.

Thanks,
John


There is no guarantee which thread will recieve the lock after thread 1
has completed it's run. Generally, the OS gives a slight preference first
to the thread with the higher priority, then to the thread that has been
waiting the longest. However, this is just a guideline, and does not
suggest a requirement on behalf of the implementation. As I said before,
there are no guarantees.

HTH

-Frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top