Question

Assume I have a class with two methods method1 and method2. I have made both of these methods synchronized. Assume a class calls method1 and the same time a different class calls method2. Will both these methods execute at the same time?

Était-ce utile?

La solution

Two threads can run two different methods at the same time. If you use synchronized it can limit the threads from running the two method at the same time, if you lock the same object. However, if your two method synchronize on different objects, they can still be as concurrent.

Autres conseils

when you set a syncronized method only one process can use that method at time, this is normally used for concurrence scenarios, the execution of your two differents methods will be executed depending on the process execution in background, so syncronized its only for restrict that two threads execute the same method at the same time. Regards

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top