M
Matthias Kaeppler
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
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