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( 039 @ParametricNullness final T value) { 040 return new Callable<T>() { 041 @Override 042 @ParametricNullness 043 public T call() { 044 return value; 045 } 046 }; 047 } 048 049 /** 050 * Creates an {@link AsyncCallable} from a {@link Callable}. 051 * 052 * <p>The {@link AsyncCallable} returns the {@link ListenableFuture} resulting from {@link 053 * ListeningExecutorService#submit(Callable)}. 054 * 055 * @since 20.0 056 */ 057 @Beta 058 @GwtIncompatible 059 public static <T extends @Nullable Object> AsyncCallable<T> asAsyncCallable( 060 final Callable<T> callable, final ListeningExecutorService listeningExecutorService) { 061 checkNotNull(callable); 062 checkNotNull(listeningExecutorService); 063 return new AsyncCallable<T>() { 064 @Override 065 public ListenableFuture<T> call() throws Exception { 066 return listeningExecutorService.submit(callable); 067 } 068 }; 069 } 070 071 /** 072 * Wraps the given callable such that for the duration of {@link Callable#call} the thread that is 073 * running will have the given name. 074 * 075 * @param callable The callable to wrap 076 * @param nameSupplier The supplier of thread names, {@link Supplier#get get} will be called once 077 * for each invocation of the wrapped callable. 078 */ 079 @GwtIncompatible // threads 080 static <T extends @Nullable Object> Callable<T> threadRenaming( 081 final Callable<T> callable, final Supplier<String> nameSupplier) { 082 checkNotNull(nameSupplier); 083 checkNotNull(callable); 084 return new Callable<T>() { 085 @Override 086 @ParametricNullness 087 public T call() throws Exception { 088 Thread currentThread = Thread.currentThread(); 089 String oldName = currentThread.getName(); 090 boolean restoreName = trySetName(nameSupplier.get(), currentThread); 091 try { 092 return callable.call(); 093 } finally { 094 if (restoreName) { 095 boolean unused = trySetName(oldName, currentThread); 096 } 097 } 098 } 099 }; 100 } 101 102 /** 103 * Wraps the given runnable such that for the duration of {@link Runnable#run} the thread that is 104 * running with have the given name. 105 * 106 * @param task The Runnable to wrap 107 * @param nameSupplier The supplier of thread names, {@link Supplier#get get} will be called once 108 * for each invocation of the wrapped callable. 109 */ 110 @GwtIncompatible // threads 111 static Runnable threadRenaming(final Runnable task, final Supplier<String> nameSupplier) { 112 checkNotNull(nameSupplier); 113 checkNotNull(task); 114 return new Runnable() { 115 @Override 116 public void run() { 117 Thread currentThread = Thread.currentThread(); 118 String oldName = currentThread.getName(); 119 boolean restoreName = trySetName(nameSupplier.get(), currentThread); 120 try { 121 task.run(); 122 } finally { 123 if (restoreName) { 124 boolean unused = trySetName(oldName, currentThread); 125 } 126 } 127 } 128 }; 129 } 130 131 /** Tries to set name of the given {@link Thread}, returns true if successful. */ 132 @GwtIncompatible // threads 133 private static boolean trySetName(final String threadName, Thread currentThread) { 134 /* 135 * setName should usually succeed, but the security manager can prohibit it. Is there a way to 136 * see if we have the modifyThread permission without catching an exception? 137 */ 138 try { 139 currentThread.setName(threadName); 140 return true; 141 } catch (SecurityException e) { 142 return false; 143 } 144 } 145}