|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.pekko.dispatch |
| 19 | + |
| 20 | +import org.apache.pekko.annotation.InternalApi |
| 21 | + |
| 22 | +import java.util |
| 23 | +import java.util.concurrent.{ Callable, Executor, ExecutorService, Future, ThreadFactory, TimeUnit } |
| 24 | + |
| 25 | +/** |
| 26 | + * A virtualized executor service that creates a new virtual thread for each task. |
| 27 | + * Will shut down the underlying executor service when this executor is being shutdown. |
| 28 | + * |
| 29 | + * INTERNAL API |
| 30 | + */ |
| 31 | +@InternalApi |
| 32 | +final class VirtualizedExecutorService( |
| 33 | + vtFactory: ThreadFactory, |
| 34 | + underlying: ExecutorService, |
| 35 | + loadMetricsProvider: Executor => Boolean, |
| 36 | + cascadeShutdown: Boolean) |
| 37 | + extends ExecutorService with LoadMetrics { |
| 38 | + require(VirtualThreadSupport.isSupported, "Virtual thread is not supported.") |
| 39 | + require(underlying != null, "Underlying executor service must not be null") |
| 40 | + require(loadMetricsProvider != null, "Load metrics provider must not be null") |
| 41 | + |
| 42 | + def this(prefix: String, |
| 43 | + underlying: ExecutorService, |
| 44 | + loadMetricsProvider: Executor => Boolean, |
| 45 | + cascadeShutdown: Boolean) = { |
| 46 | + this(VirtualThreadSupport.newVirtualThreadFactory(prefix), underlying, loadMetricsProvider, cascadeShutdown) |
| 47 | + } |
| 48 | + |
| 49 | + private val executor = VirtualThreadSupport.newThreadPerTaskExecutor(vtFactory) |
| 50 | + |
| 51 | + override def atFullThrottle(): Boolean = loadMetricsProvider(this) |
| 52 | + |
| 53 | + override def shutdown(): Unit = { |
| 54 | + executor.shutdown() |
| 55 | + if (cascadeShutdown) { |
| 56 | + underlying.shutdown() |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + override def shutdownNow(): util.List[Runnable] = { |
| 61 | + val r = executor.shutdownNow() |
| 62 | + if (cascadeShutdown) { |
| 63 | + underlying.shutdownNow() |
| 64 | + } |
| 65 | + r |
| 66 | + } |
| 67 | + |
| 68 | + override def isShutdown: Boolean = { |
| 69 | + if (cascadeShutdown) { |
| 70 | + executor.isShutdown || underlying.isShutdown |
| 71 | + } else { |
| 72 | + executor.isShutdown |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + override def isTerminated: Boolean = { |
| 77 | + if (cascadeShutdown) { |
| 78 | + executor.isTerminated && underlying.isTerminated |
| 79 | + } else { |
| 80 | + executor.isTerminated |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + override def awaitTermination(timeout: Long, unit: TimeUnit): Boolean = { |
| 85 | + if (cascadeShutdown) { |
| 86 | + executor.awaitTermination(timeout, unit) && underlying.awaitTermination(timeout, unit) |
| 87 | + } else { |
| 88 | + executor.awaitTermination(timeout, unit) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + override def submit[T](task: Callable[T]): Future[T] = { |
| 93 | + executor.submit(task) |
| 94 | + } |
| 95 | + |
| 96 | + override def submit[T](task: Runnable, result: T): Future[T] = { |
| 97 | + executor.submit(task, result) |
| 98 | + } |
| 99 | + |
| 100 | + override def submit(task: Runnable): Future[_] = { |
| 101 | + executor.submit(task) |
| 102 | + } |
| 103 | + |
| 104 | + override def invokeAll[T](tasks: util.Collection[_ <: Callable[T]]): util.List[Future[T]] = { |
| 105 | + executor.invokeAll(tasks) |
| 106 | + } |
| 107 | + |
| 108 | + override def invokeAll[T]( |
| 109 | + tasks: util.Collection[_ <: Callable[T]], timeout: Long, unit: TimeUnit): util.List[Future[T]] = { |
| 110 | + executor.invokeAll(tasks, timeout, unit) |
| 111 | + } |
| 112 | + |
| 113 | + override def invokeAny[T](tasks: util.Collection[_ <: Callable[T]]): T = { |
| 114 | + executor.invokeAny(tasks) |
| 115 | + } |
| 116 | + |
| 117 | + override def invokeAny[T](tasks: util.Collection[_ <: Callable[T]], timeout: Long, unit: TimeUnit): T = { |
| 118 | + executor.invokeAny(tasks, timeout, unit) |
| 119 | + } |
| 120 | + |
| 121 | + override def execute(command: Runnable): Unit = { |
| 122 | + executor.execute(command) |
| 123 | + } |
| 124 | +} |
0 commit comments