com.oracle.coherence.concurrent.config.builders.CachedBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of coherence-concurrent Show documentation
Show all versions of coherence-concurrent Show documentation
Utility classes commonly useful in concurrent programming within a Coherence Cluster.
/*
* Copyright (c) 2021, 2024, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package com.oracle.coherence.concurrent.config.builders;
import com.oracle.coherence.concurrent.config.NamedExecutorService;
import com.oracle.coherence.concurrent.executor.util.NamedThreadFactory;
import com.tangosol.coherence.config.ParameterList;
import com.tangosol.coherence.config.builder.ParameterizedBuilder;
import com.tangosol.config.expression.ParameterResolver;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.function.Supplier;
/**
* A {@link ParameterizedBuilder} for constructing a {@link NamedExecutorService}
* wrapper that will construct a cached thread pool.
*
* @author rl 11.26.21
* @since 21.12
*/
public class CachedBuilder
extends AbstractExecutorWithFactoryBuilder
{
// ----- ParameterizedBuilder interface ---------------------------------
@Override
public NamedExecutorService realize(ParameterResolver resolver, ClassLoader loader, ParameterList listParameters)
{
String sName = m_name.evaluate(resolver);
ThreadFactory factory = instantiateThreadFactory(sName, resolver,
loader, listParameters);
Supplier supplier = factory == null
? Executors::newCachedThreadPool
: () -> Executors.newCachedThreadPool(factory);
NamedExecutorService service = new NamedExecutorService(sName, description(factory), supplier);
register(service);
return service;
}
// ----- helper methods -------------------------------------------------
/**
* Creates the description for this executor.
*
* @param factory the {@link ThreadFactory}, if any
*
* @return the description for this executor
*/
protected String description(ThreadFactory factory)
{
String sFactory = factory == null || NamedThreadFactory.class.equals(factory.getClass())
? "default"
: factory.getClass().getName();
return String.format("CachedThreadPool(ThreadFactory=%s)", sFactory);
}
}