3434import java .security .PrivilegedAction ;
3535import java .util .ArrayDeque ;
3636import java .util .Deque ;
37+ import java .util .function .BiFunction ;
38+ import java .util .function .Function ;
3739import java .util .HashSet ;
3840import java .util .Objects ;
3941import java .util .Map ;
@@ -483,28 +485,36 @@ public int getCounter() {
483485 new ConcurrentHashMap <>();
484486
485487 private static void acquireNativeLibraryLock (String libraryName ) {
486- nativeLibraryLockMap .compute (libraryName , (name , currentLock ) -> {
487- if (currentLock == null ) {
488- currentLock = new CountedLock ();
488+ nativeLibraryLockMap .compute (libraryName ,
489+ new BiFunction <>() {
490+ public CountedLock apply (String name , CountedLock currentLock ) {
491+ if (currentLock == null ) {
492+ currentLock = new CountedLock ();
493+ }
494+ // safe as compute BiFunction<> is executed atomically
495+ currentLock .increment ();
496+ return currentLock ;
497+ }
489498 }
490- // safe as compute lambda is executed atomically
491- currentLock .increment ();
492- return currentLock ;
493- }).lock ();
499+ ).lock ();
494500 }
495501
496502 private static void releaseNativeLibraryLock (String libraryName ) {
497- CountedLock lock = nativeLibraryLockMap .computeIfPresent (libraryName , (name , currentLock ) -> {
498- if (currentLock .getCounter () == 1 ) {
499- // unlock and release the object if no other threads are queued
500- currentLock .unlock ();
501- // remove the element
502- return null ;
503- } else {
504- currentLock .decrement ();
505- return currentLock ;
503+ CountedLock lock = nativeLibraryLockMap .computeIfPresent (libraryName ,
504+ new BiFunction <>() {
505+ public CountedLock apply (String name , CountedLock currentLock ) {
506+ if (currentLock .getCounter () == 1 ) {
507+ // unlock and release the object if no other threads are queued
508+ currentLock .unlock ();
509+ // remove the element
510+ return null ;
511+ } else {
512+ currentLock .decrement ();
513+ return currentLock ;
514+ }
515+ }
506516 }
507- } );
517+ );
508518 if (lock != null ) {
509519 lock .unlock ();
510520 }
@@ -521,7 +531,11 @@ private static final class NativeLibraryContext {
521531 private static Deque <NativeLibraryImpl > current () {
522532 return nativeLibraryThreadContext .computeIfAbsent (
523533 Thread .currentThread (),
524- t -> new ArrayDeque <>(8 ));
534+ new Function <>() {
535+ public Deque <NativeLibraryImpl > apply (Thread t ) {
536+ return new ArrayDeque <>(8 );
537+ }
538+ });
525539 }
526540
527541 private static NativeLibraryImpl peek () {
0 commit comments