1

If class A has two synchronized methods, say, methodA and methodB. If one thread is accessing methodA, can another thread access methodB? According to my understanding, when accessing methodA, the thread get lock of the object, the other thread shouldn't be able to access methodB at the same time. But this will downgrade the performance a lot, right? And, I seemed used to read one article that says the other thread can access methodB. Which one is correct?

user1801838
  • 137
  • 1
  • 1
  • 4
  • Depends on what you use for the lock. If the method is synchronized, only one thread has access to it at the time of execution. – Shark Apr 02 '13 at 10:01

2 Answers2

5

If class A has two synchronized methods, say, methodA and methodB. If one thread is accessing methodA, can another thread access methodB? According to my understanding, when accessing methodA, the thread get lock of the object, the other thread shouldn't be able to access methodB at the same time.

That's right; if one thread holds the lock to the object, other threads have to wait before they can get the lock. Ofcourse this is only the case if both threads are calling methods on the exact same object.

But this will downgrade the performance a lot, right?

It might affect performance, but sometimes it's necessary, for example to prevent two threads from modifying the same data concurrently.

And, I seemed used to read one article that says the other thread can access methodB. Which one is correct?

The first is correct, the second is incorrect.

Jesper
  • 202,709
  • 46
  • 318
  • 350
1

But this will downgrade the performance a lot, right?

It might, or it might not. It depends on the nature of the application. The synchronization could have near-zero cost, or it could be a major bottleneck, effectively turning a multithreaded application into a single-threaded one.

See Amdahl's Law.

And, I seemed used to read one article that says the other thread can access methodB.

No other thread can call methodB on the same object while methodA is running. Of course, other threads are free to call methodB on other instances of the same class.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • > near-zero cost - syncronization is not nearly near-zero, even if it is not needed. You can compare performance of ArrayList and Vector classes to see that – popfalushi Apr 02 '13 at 10:06
  • To prove a point I made an experiment. Code and result in the commentaries below code: http://pastebin.com/UzxdzvV4 . Arraylist.add is 2-3 times faster than Vector.add, which is synchronized version of arraylist. – popfalushi Apr 02 '13 at 10:15
  • @popfalushi say you have tons of threads doing slow disk operations and storing their results in a single object... the overhead of synchronization would be near-zero, right? – Vincent van der Weele Apr 02 '13 at 10:21
  • With that logic, using python instead of java does not degrade performance. But it does. If some app does not suffer from performance degradation (simple script, for example) than other will be severely affected by it. I remember one case when change from Vector to ArrayList improved performance greatly (this was former C++ programmer, who used Vector instead of ArrayList because name was familiar). – popfalushi Apr 02 '13 at 10:26