1

I don't really see the difference of execution between

public void foo_fct( Table Tbl, Account act)
{
  synchronized(Tbl) {
    ...
  }
}

and this :

public void foo_fct( Table Tbl, Account act)
{
  synchronized(act) {
    ...
  }
}

I mean, under the hood, the JVM must use lock() and unlock() I guess? So whatever it happens (act or Tbl) I will be locking in the same way no ?

update Ok, I understand now with your help and Java doc :

"...Every object has an intrinsic lock associated with it..."

and "synchronized statements..."

2

3 Answers 3

1

Yes, there is a difference, since you never know which other threads are using sychronized on the Table or the Account.

Ask yourself the question- are you trying to prevent other mutable operations on the Table or the Account for the duration of execution of the sychronized block in foo_fct method?

Sign up to request clarification or add additional context in comments.

Comments

0

If table and accounts are organized in fixed exclusive pairs meaning no foo_fct on a table x will be called with different account instances by different concurrent threads (and vice versa) and there no other parts of the code that make other synchronized operations on tables or accounts in your program concurrently then both ways to lock are equivalent.

Otherwise both approaches would be different.

Comments

0

The synchronized(var) block will make difference based on the ... block that you omitted, i.e. on which variable you already need to synchronize concurrent threads access.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.