سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top