001/* 002 * Copyright (C) 2009 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.util.concurrent; 016 017import static com.google.common.base.Preconditions.checkNotNull; 018 019import com.google.common.annotations.GwtCompatible; 020import com.google.common.annotations.GwtIncompatible; 021import com.google.common.annotations.J2ktIncompatible; 022import com.google.common.base.Supplier; 023import java.util.concurrent.Callable; 024import org.checkerframework.checker.nullness.qual.Nullable; 025 026/** 027 * Static utility methods pertaining to the {@link Callable} interface. 028 * 029 * @author Isaac Shum 030 * @since 1.0 031 */ 032@GwtCompatible(emulated = true) 033@ElementTypesAreNonnullByDefault 034public final class Callables { 035 private Callables() {} 036 037 /** Creates a {@code Callable} which immediately returns a preset value each time it is called. */ 038 public static <T extends @Nullable Object> Callable<T> returning(@ParametricNullness T value) { 039 return () -> value; 040 } 041 042 /** 043 * Creates an {@link AsyncCallable} from a {@link Callable}. 044 * 045 * <p>The {@link AsyncCallable} returns the {@link ListenableFuture} resulting from {@link 046 * ListeningExecutorService#submit(Callable)}. 047 * 048 * @since 20.0 049 */ 050 @J2ktIncompatible 051 @GwtIncompatible 052 public static <T extends @Nullable Object> AsyncCallable<T> asAsyncCallable( 053 Callable<T> callable, ListeningExecutorService listeningExecutorService) { 054 checkNotNull(callable); 055 checkNotNull(listeningExecutorService); 056 return () -> listeningExecutorService.submit(callable); 057 } 058 059 /** 060 * Wraps the given callable such that for the duration of {@link Callable#call} the thread that is 061 * running will have the given name. 062 * 063 * @param callable The callable to wrap 064 * @param nameSupplier The supplier of thread names, {@link Supplier#get get} will be called once 065 * for each invocation of the wrapped callable. 066 */ 067 @J2ktIncompatible 068 @GwtIncompatible // threads 069 static <T extends @Nullable Object> Callable<T> threadRenaming( 070 Callable<T> callable, Supplier<String> nameSupplier) { 071 checkNotNull(nameSupplier); 072 checkNotNull(callable); 073 return () -> { 074 Thread currentThread = Thread.currentThread(); 075 String oldName = currentThread.getName(); 076 boolean restoreName = trySetName(nameSupplier.get(), currentThread); 077 try { 078 return callable.call(); 079 } finally { 080 if (restoreName) { 081 boolean unused = trySetName(oldName, currentThread); 082 } 083 } 084 }; 085 } 086 087 /** 088 * Wraps the given runnable such that for the duration of {@link Runnable#run} the thread that is 089 * running with have the given name. 090 * 091 * @param task The Runnable to wrap 092 * @param nameSupplier The supplier of thread names, {@link Supplier#get get} will be called once 093 * for each invocation of the wrapped callable. 094 */ 095 @J2ktIncompatible 096 @GwtIncompatible // threads 097 static Runnable threadRenaming(Runnable task, Supplier<String> nameSupplier) { 098 checkNotNull(nameSupplier); 099 checkNotNull(task); 100 return () -> { 101 Thread currentThread = Thread.currentThread(); 102 String oldName = currentThread.getName(); 103 boolean restoreName = trySetName(nameSupplier.get(), currentThread); 104 try { 105 task.run(); 106 } finally { 107 if (restoreName) { 108 boolean unused = trySetName(oldName, currentThread); 109 } 110 } 111 }; 112 } 113 114 /** Tries to set name of the given {@link Thread}, returns true if successful. */ 115 @J2ktIncompatible 116 @GwtIncompatible // threads 117 private static boolean trySetName(String threadName, Thread currentThread) { 118 /* 119 * setName should usually succeed, but the security manager can prohibit it. Is there a way to 120 * see if we have the modifyThread permission without catching an exception? 121 */ 122 try { 123 currentThread.setName(threadName); 124 return true; 125 } catch (SecurityException e) { 126 return false; 127 } 128 } 129}