简体   繁体   中英

Will both method execute at same time

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM