-3

is the following statement correct:

" There shouldn't be any thread interference between two synchronized methods in 2 different classes . So they can run concurrently without any problems."

Thanks for your time

samW
  • 3
  • 3
  • yup, seems good to me – Angry Red Panda Feb 12 '18 at 10:18
  • @AngryRedPanda I edited it, Is it still correct ? – samW Feb 12 '18 at 10:18
  • What does, "run concurrently without any problems" mean? Does it mean nothing will prevent calls to the two methods from overlapping in different threads? or does it mean that the programmer does not need to worry about data accesses in one method interfering with data accesses in the other? – Solomon Slow Feb 12 '18 at 11:30

2 Answers2

1

You misunderstood the concept a little bit. Collisions happen when two (or more) threads simultaneously try to make a change on the same data or when one of them tries the read the data while the other thread is trying to change it.

When two thread tries to change the shared resource simultaneously, a race condition occurs. Check out this link to learn more about Race Condition.

In order to prevent this kind of problems, you need to guard the shared resource for simultaneous changes. Mutexes and semaphores are invented for this purpose: To lock the shared resource for the other threads, when one thread is currently making a change on it. For this purpose, Java uses the synchronized keyword. You can read more about Synchronized in Java using the link.

Note that, using the synchronized keyword will not eliminate all of the synchronization related issues, but it is a good starting point.

Can Bayar
  • 497
  • 4
  • 16
  • Thanks so much, I edited the question a bit, can u include a small sentence regarding the change ? is what I typed correct ? – samW Feb 12 '18 at 10:22
  • @samW I don't think the question you have intended at the beginning is the same with what you are asking right now. Your question now asks that usage of synchronized keyword in different classes will not affect each other. Is this what you are trying to ask? – Can Bayar Feb 12 '18 at 10:25
1

That is way too vague. A few pointers:

  • "how does synchronization work in Java": There are a couple of mechanisms, the question seems to be about the synchronized keyword. This works by marking "critical sections" that must not be executed by more than one thread at the same time, and having the threads "lock" a monitor object while they are in that section (so that all other threads wait).

  • synchronized methods synchronize on the object instance (or class object in case of a static method). So methods in different classes do not synchronize with each-other that way. They will run concurrently.

  • you can use the synchronized keyword to synchronize blocks on any other monitor object. This way, methods in different classes can still be synchronized with each-other.

  • "can run concurrently without problems" is not guaranteed just by having some synchronization (or lack thereof). You need to see what mutable state these methods (directly or indirectly) try to access (and who else does the same) to see what kind of concurrency control is necessary.

Thilo
  • 257,207
  • 101
  • 511
  • 656