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.Beta; 020import com.google.common.annotations.GwtCompatible; 021import com.google.common.annotations.GwtIncompatible; 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 @Beta 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 @GwtIncompatible // threads 068 static <T extends @Nullable Object> Callable<T> threadRenaming( 069 Callable<T> callable, Supplier<String> nameSupplier) { 070 checkNotNull(nameSupplier); 071 checkNotNull(callable); 072 return () -> { 073 Thread currentThread = Thread.currentThread(); 074 String oldName = currentThread.getName(); 075 boolean restoreName = trySetName(nameSupplier.get(), currentThread); 076 try { 077 return callable.call(); 078 } finally { 079 if (restoreName) { 080 boolean unused = trySetName(oldName, currentThread); 081 } 082 } 083 }; 084 } 085 086 /** 087 * Wraps the given runnable such that for the duration of {@link Runnable#run} the thread that is 088 * running with have the given name. 089 * 090 * @param task The Runnable to wrap 091 * @param nameSupplier The supplier of thread names, {@link Supplier#get get} will be called once 092 * for each invocation of the wrapped callable. 093 */ 094 @GwtIncompatible // threads 095 static Runnable threadRenaming(Runnable task, Supplier<String> nameSupplier) { 096 checkNotNull(nameSupplier); 097 checkNotNull(task); 098 return () -> { 099 Thread currentThread = Thread.currentThread(); 100 String oldName = currentThread.getName(); 101 boolean restoreName = trySetName(nameSupplier.get(), currentThread); 102 try { 103 task.run(); 104 } finally { 105 if (restoreName) { 106 boolean unused = trySetName(oldName, currentThread); 107 } 108 } 109 }; 110 } 111 112 /** Tries to set name of the given {@link Thread}, returns true if successful. */ 113 @GwtIncompatible // threads 114 private static boolean trySetName(String threadName, Thread currentThread) { 115 /* 116 * setName should usually succeed, but the security manager can prohibit it. Is there a way to 117 * see if we have the modifyThread permission without catching an exception? 118 */ 119 try { 120 currentThread.setName(threadName); 121 return true; 122 } catch (SecurityException e) { 123 return false; 124 } 125 } 126}