Skip to content

Commit

Permalink
COH-29642 - Provide a default ThreadFactory implemetation for named E…
Browse files Browse the repository at this point in the history
…xecutors (merge ce/main -> ce/23.09 @ 107362)

[git-p4: depot-paths = "//dev/coherence-ce/release/coherence-ce-v23.09/": change = 107363]
  • Loading branch information
rlubke committed Mar 10, 2024
1 parent 33bf165 commit 117137b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
* https://oss.oracle.com/licenses/upl.
*/
package com.oracle.coherence.concurrent.config.builders;

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.annotation.Injectable;

import com.tangosol.config.expression.Parameter;
import com.tangosol.config.expression.ParameterResolver;
import java.util.concurrent.ThreadFactory;

/**
Expand Down Expand Up @@ -38,6 +42,35 @@ public void setInstanceBuilder(ParameterizedBuilder<ThreadFactory> bldr)
m_bldr = bldr;
}

// ----- helper methods -------------------------------------------------

/**
* Creates and returns a ThreadFactory.
*
* @param sName the name to use if no user-defined
* ThreadFactory is defined
* @param resolver the {@link ParameterResolver} for resolving
* named {@link Parameter}s
* @param loader the {@link ClassLoader} for loading any
* necessary classes and if <code>null</code> the
* {@link ClassLoader} used to load the builder
* will be used instead
* @param listParameters an optional {@link ParameterList}
* (may be <code>null</code>) to be used for
* realizing the instance, eg: used as constructor
* parameters
*
* @return the {@link ThreadFactory}
*/
protected ThreadFactory instantiateThreadFactory(String sName,
ParameterResolver resolver, ClassLoader loader, ParameterList listParameters)
{
return m_bldr == null
? new NamedThreadFactory(sName)
: m_bldr.realize(resolver, loader, listParameters);

}

// ----- data members ---------------------------------------------------

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
* 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;
Expand Down Expand Up @@ -36,9 +37,8 @@ public class CachedBuilder
public NamedExecutorService realize(ParameterResolver resolver, ClassLoader loader, ParameterList listParameters)
{
String sName = m_name.evaluate(resolver);
ThreadFactory factory = m_bldr == null
? null
: m_bldr.realize(resolver, loader, listParameters);
ThreadFactory factory = instantiateThreadFactory(sName, resolver,
loader, listParameters);
Supplier<ExecutorService> supplier = factory == null
? Executors::newCachedThreadPool
: () -> Executors.newCachedThreadPool(factory);
Expand All @@ -60,7 +60,9 @@ public NamedExecutorService realize(ParameterResolver resolver, ClassLoader load
*/
protected String description(ThreadFactory factory)
{
String sFactory = factory == null ? "default" : factory.getClass().getName();
String sFactory = factory == null || NamedThreadFactory.class.equals(factory.getClass())
? "default"
: factory.getClass().getName();

return String.format("CachedThreadPool(ThreadFactory=%s)", sFactory);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
* 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;
Expand Down Expand Up @@ -40,9 +41,8 @@ public NamedExecutorService realize(ParameterResolver resolver, ClassLoader load
{
String sName = m_name.evaluate(resolver);
int cThreadCount = m_threadCount.evaluate(resolver);
ThreadFactory factory = m_bldr == null
? null
: m_bldr.realize(resolver, loader, listParameters);
ThreadFactory factory = instantiateThreadFactory(sName, resolver,
loader, listParameters);
Supplier<ExecutorService> supplier = factory == null
? () -> Executors.newFixedThreadPool(cThreadCount)
: () -> Executors.newFixedThreadPool(cThreadCount, factory);
Expand Down Expand Up @@ -78,7 +78,9 @@ public void setThreadCount(Expression<Integer> threadCount)
*/
protected String description(int cThreadCount, ThreadFactory factory)
{
String sFactory = factory == null ? "default" : factory.getClass().getName();
String sFactory = factory == null || NamedThreadFactory.class.equals(factory.getClass())
? "default"
: factory.getClass().getName();

return String.format("FixedThreadPool(ThreadCount=%s, ThreadFactory=%s)", cThreadCount, sFactory);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
* 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;
Expand Down Expand Up @@ -36,9 +37,8 @@ public class SingleBuilder
public NamedExecutorService realize(ParameterResolver resolver, ClassLoader loader, ParameterList listParameters)
{
String sName = m_name.evaluate(resolver);
ThreadFactory factory = m_bldr == null
? null
: m_bldr.realize(resolver, loader, listParameters);
ThreadFactory factory = instantiateThreadFactory(sName, resolver,
loader, listParameters);
Supplier<ExecutorService> supplier = factory == null
? Executors::newSingleThreadExecutor
: () -> Executors.newSingleThreadExecutor(factory);
Expand All @@ -60,7 +60,9 @@ public NamedExecutorService realize(ParameterResolver resolver, ClassLoader load
*/
protected String description(ThreadFactory factory)
{
String sFactory = factory == null ? "default" : factory.getClass().getName();
String sFactory = factory == null || NamedThreadFactory.class.equals(factory.getClass())
? "default"
: factory.getClass().getName();

return String.format("SingleThreaded(ThreadFactory=%s)", sFactory);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 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.executor.util;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;


/**
* A {@link ThreadFactory} implementation for used with {@code named}
* {@code Executors}. Each new thread's name will be composed by using
* the name of the executor service as the prefix and a monotonically
* increasing integer as the suffix.
*
* @author rl 3.8.2024
* @since 15.1.1.0
*/
public class NamedThreadFactory
implements ThreadFactory
{
// ----- constructors ---------------------------------------------------

/**
* Constructs a new {@code NamedThreadFactory} for the specified
* executor name.
*
* @param f_sName the executor name
*/
public NamedThreadFactory(String f_sName)
{
if (f_sName == null || f_sName.isEmpty())
{
throw new IllegalArgumentException("A name must be specified");
}

this.f_sName = f_sName;
}

// ----- ThreadFactory interface ----------------------------------------

@Override
public Thread newThread(Runnable r)
{
return new Thread(r, "CES:" + f_sName + '-' + f_counter.incrementAndGet());
}

// ----- data members ---------------------------------------------------

protected final AtomicInteger f_counter = new AtomicInteger();

protected final String f_sName;
}

0 comments on commit 117137b

Please sign in to comment.