001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.util.concurrent; 018 019import org.slf4j.Logger; 020import org.slf4j.LoggerFactory; 021 022/** 023 * Defines the existing type of threads. The virtual threads can be enabled with the system property 024 * {@code camel.threads.virtual.enabled} set to {@code true}, or via the Camel Main configuration property 025 * {@code camel.main.virtualThreadsEnabled}. The default value is {@code false} which means that platform threads are 026 * used by default. 027 * <p> 028 * The thread type is resolved lazily on first access, allowing configuration properties to set the system property 029 * before the type is determined. 030 */ 031public enum ThreadType { 032 PLATFORM, 033 VIRTUAL; 034 035 private static final Logger LOG = LoggerFactory.getLogger(ThreadType.class); 036 private static volatile ThreadType current; 037 038 public static ThreadType current() { 039 ThreadType type = current; 040 if (type == null) { 041 synchronized (ThreadType.class) { 042 type = current; 043 if (type == null) { 044 type = Boolean.getBoolean("camel.threads.virtual.enabled") ? VIRTUAL : PLATFORM; 045 current = type; 046 if (type == VIRTUAL) { 047 LOG.info("The type of thread detected is: {}", type); 048 } else { 049 LOG.debug("The type of thread detected is: {}", type); 050 } 051 } 052 } 053 } 054 return type; 055 } 056 057 /** 058 * Directly enables virtual threads by setting the cached type to {@code VIRTUAL}. 059 * <p> 060 * This must be called before any thread pools are created, ideally during early bootstrap, to ensure the cached 061 * value reflects the configured intent regardless of the order in which {@link #current()} was previously invoked. 062 */ 063 public static void enable() { 064 synchronized (ThreadType.class) { 065 current = VIRTUAL; 066 } 067 LOG.info("The type of thread enabled is: {}", VIRTUAL); 068 } 069}