Waiting For A Synchronized Method

Discussion in 'Java' started by cppaddict, Aug 23, 2004.

  1. cppaddict

    cppaddict Guest

    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
    cppaddict, Aug 23, 2004
    #1
    1. Advertising

  2. cppaddict

    Chris Uppal Guest

    cppaddict wrote:

    > 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
    Chris Uppal, Aug 23, 2004
    #2
    1. Advertising

  3. cppaddict

    Frank Guest

    On Mon, 23 Aug 2004 10:23:03 GMT, cppaddict <> wrote:

    > 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
    Frank, Aug 24, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Justin Fowler
    Replies:
    4
    Views:
    398
    Joona I Palaste
    Aug 22, 2003
  2. Jerry
    Replies:
    4
    Views:
    131,811
    tonni
    Aug 11, 2010
  3. Pep
    Replies:
    6
    Views:
    29,261
  4. dmcreyno
    Replies:
    9
    Views:
    9,559
    Mark Space
    Jun 27, 2006
  5. ankur
    Replies:
    4
    Views:
    1,433
    Eric Sosman
    Nov 28, 2008
Loading...

Share This Page