private static <T,R>R accessSafely(Lock lock,List<T> list,Function<List<T>,R> listFunction){
  final R returnValue;
  lock.lock();
  try {
    returnValue=listFunction.apply(list);
  }
  finally {
    lock.unlock();
  }
  return returnValue;
}

private static < T , R > R accessSafely ( Lock lock , List < T > list , Function < List < T > , R > listFunction ) { final R returnValue ; lock . lock ( ) ; try { returnValue = listFunction . apply ( list ) ; } finally { lock . unlock ( ) ; } return returnValue ; }
locks the resource with the specified lock and performs the function . when the function is complete then the lock unlocked again .
