public final List < Lock > lockAll ( Object ... keys ) { List < Lock > locks = new ArrayList <> ( keys . length ) ; for ( Object key : keys ) { Lock lock = this . locks . get ( key ) ; locks . add ( lock ) ; } Collections . sort ( locks , ( a , b ) -> { int diff = a . hashCode ( ) - b . hashCode ( ) ; if ( diff == 0 && a != b ) { diff = this . indexOf ( a ) - this . indexOf ( b ) ; assert diff != 0 ; } return diff ; } ) ; for ( int i = 0 ; i < locks . size ( ) ; i ++ ) { locks . get ( i ) . lock ( ) ; } return Collections . unmodifiableList ( locks ) ; }
Lock a list of object with sorted order