Question about synchronized
- From: Matthias Kaeppler <void@xxxxxxxx>
- Date: Sat, 12 Nov 2005 14:26:46 +0100
Hi,
say I have two different methods M1 and M2 which work on the same object O, whereas M1 is declared 'synchronized' while M2 is not.
Now imagine I start two threads A and B, which keep calling M1 and M2 respectively in an endless loop.
As far as I can tell, if M1 is called by A, a lock is acquired on O and thus each call to M2 in B's context will result in the VM inserting B in the wait set until M1 has finished (this is only because M1 is synchronized).
But what if M2 is called on O in B's context /first/ (remember it's not synchronized), and no lock is acquired on O, and while it's still computing, the VM decides to give A the CPU?
Will M1 now be able to work on O, even though M2 hasn't finished yet?
In other words, is in my case of two functions, two threads and one object the program only thread safe if /both/ methods M1 and M2 are declared synchronized? And what effects could arise from one being not not while the other is?
Thanks, Matthias .
- Follow-Ups:
- Re: Question about synchronized
- From: John C. Bollinger
- Re: Question about synchronized
- From: Steve Horsley
- Re: Question about synchronized
- From: Roedy Green
- Re: Question about synchronized
- Prev by Date: Re: JNI_CreateJavaVM
- Next by Date: Re: Efficient String Manipulation ?
- Previous by thread: converting integers to BigDecimals
- Next by thread: Re: Question about synchronized
- Index(es):
Relevant Pages
|