Code example for ThreadFactoryBuilder

0
import java.util.concurrent.TimeUnit;
 
public class FileUtils 
{ 
    private static final int TEMP_DIR_ATTEMPTS = 10000;
    private static final Executor executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("tar-command-%s").build());
 
    public static void createTar(File sourceDirectory, File tarFile)
            throws CommandFailedException 
    { 
        Preconditions.checkNotNull(sourceDirectory, "sourceDirectory is null");
        Preconditions.checkArgument(sourceDirectory.isDirectory(), "sourceDirectory is not a directory: " + sourceDirectory.getAbsolutePath());
        Preconditions.checkNotNull(tarFile, "tarFile is null");
 
        tarFile.getParentFile().mkdirs();
        new Command("tar", "zcf", tarFile.getAbsolutePath(), sourceDirectory.getName())
                .setDirectory(sourceDirectory.getParent())
                .setTimeLimit(5, TimeUnit.MINUTES)
                .execute(executor);
    }