The mechanism of synchronization of Josephson arrays coupled to a cavity