private static ThreadFactory build()

in src/main/java/com/epam/deltix/thread/affinity/AffinityThreadFactoryBuilder.java [108:151]


    private static ThreadFactory build(AffinityThreadFactoryBuilder builder) {
        final String nameFormat = builder.nameFormat;
        final Boolean daemon = builder.daemon;
        final Integer priority = builder.priority;
        final Thread.UncaughtExceptionHandler uncaughtExceptionHandler =
                builder.uncaughtExceptionHandler;

        ThreadFactory baseThreadFactory = (builder.backingThreadFactory != null)
                ? builder.backingThreadFactory
                : Executors.defaultThreadFactory();

        final ThreadFactory backingThreadFactory;

        // Note: Here we choose one affinity level for all threads of this thread factory. However we can change it by using another wrapper (if needed).
        AffinityLayout affinity = determineEffectiveAffinity(builder);
        if (affinity == null) {
            backingThreadFactory = baseThreadFactory;
        } else {
            backingThreadFactory = new PinnedThreadFactoryWrapper(baseThreadFactory, affinity);
        }

        final AtomicLong count = (nameFormat != null) ? new AtomicLong(0) : null;
        return new ThreadFactory() {
            @Override
            public Thread newThread(Runnable runnable) {
                Thread thread = backingThreadFactory.newThread(runnable);
                if (nameFormat != null) {
                    thread.setName(format(nameFormat, count.getAndIncrement()));
                }
                if (daemon != null) {
                    thread.setDaemon(daemon);
                }
                if (priority != null) {
                    thread.setPriority(priority);
                }
                if (uncaughtExceptionHandler != null) {
                    thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
                }

                LOG.debug("Created thread with name: {}", thread.getName());
                return thread;
            }
        };
    }