@ Override public void supplierChanged ( SupplierEvent supplierEvent ) { SupplierEvent . Type type = supplierEvent . type ( ) ; @ SuppressWarnings ( "unchecked" ) Supplier < T > supplier = ( Supplier < T > ) supplierEvent . supplier ( ) ; switch ( type ) { case ADD : if ( supplierReference . compareAndSet ( null , supplier ) ) { supplierFutureRef . get ( ) . complete ( supplier ) ; } break ; case REMOVE : if ( supplierReference . compareAndSet ( supplier , null ) ) { supplierFutureRef . set ( new CompletableFuture <> ( ) ) ; } break ; default : throw new IllegalStateException ( "Unknown supplier event: " + supplierEvent ) ; } }
there no thread safety issue because the supplierFutureRef is only used for the asynchronous approach . The risk is small enough to avoid to introduce more complexity .