Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Waiting For A Synchronized Method

Reply
Thread Tools

Waiting For A Synchronized Method

 
 
cppaddict
Guest
Posts: n/a
 
      08-23-2004
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
 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      08-23-2004
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


 
Reply With Quote
 
 
 
 
Frank
Guest
Posts: n/a
 
      08-24-2004
On Mon, 23 Aug 2004 10:23:03 GMT, cppaddict <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Synchronized Block v.s. Synchronized Method Jerry Java 4 08-11-2010 02:34 PM
Question on synchronized and non synchronized methods ankur Java 4 11-28-2008 09:56 PM
synchronized block in synchronized static method dmcreyno Java 9 06-27-2006 07:43 PM
Use of synchronized variables over synchronized methods? Pep Java 6 08-15-2005 01:29 PM
Re: criteria to decide whether method must be synchronized Justin Fowler Java 4 08-22-2003 08:25 AM



Advertisments