|
| 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, ExecutorService, Future, 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(prefix: String, underlying: ExecutorService) extends ExecutorService { |
| 33 | + require(prefix ne null, "Parameter prefix must not be null or empty") |
| 34 | + require(underlying ne null, "Parameter underlying must not be null") |
| 35 | + |
| 36 | + private val executor = VirtualThreadSupportReflect.newThreadPerTaskExecutor(prefix, underlying) |
| 37 | + |
| 38 | + override def shutdown(): Unit = { |
| 39 | + executor.shutdown() |
| 40 | + underlying.shutdown() |
| 41 | + } |
| 42 | + |
| 43 | + override def shutdownNow(): util.List[Runnable] = { |
| 44 | + executor.shutdownNow() |
| 45 | + underlying.shutdownNow() |
| 46 | + } |
| 47 | + |
| 48 | + override def isShutdown: Boolean = { |
| 49 | + executor.isShutdown || underlying.isShutdown |
| 50 | + } |
| 51 | + |
| 52 | + override def isTerminated: Boolean = { |
| 53 | + executor.isTerminated && underlying.isTerminated |
| 54 | + } |
| 55 | + |
| 56 | + override def awaitTermination(timeout: Long, unit: TimeUnit): Boolean = { |
| 57 | + executor.awaitTermination(timeout, unit) && underlying.awaitTermination(timeout, unit) |
| 58 | + } |
| 59 | + |
| 60 | + override def submit[T](task: Callable[T]): Future[T] = { |
| 61 | + executor.submit(task) |
| 62 | + } |
| 63 | + |
| 64 | + override def submit[T](task: Runnable, result: T): Future[T] = { |
| 65 | + executor.submit(task, result) |
| 66 | + } |
| 67 | + |
| 68 | + override def submit(task: Runnable): Future[_] = { |
| 69 | + executor.submit(task) |
| 70 | + } |
| 71 | + |
| 72 | + override def invokeAll[T](tasks: util.Collection[_ <: Callable[T]]): util.List[Future[T]] = { |
| 73 | + executor.invokeAll(tasks) |
| 74 | + } |
| 75 | + |
| 76 | + override def invokeAll[T]( |
| 77 | + tasks: util.Collection[_ <: Callable[T]], timeout: Long, unit: TimeUnit): util.List[Future[T]] = { |
| 78 | + executor.invokeAll(tasks, timeout, unit) |
| 79 | + } |
| 80 | + |
| 81 | + override def invokeAny[T](tasks: util.Collection[_ <: Callable[T]]): T = { |
| 82 | + executor.invokeAny(tasks) |
| 83 | + } |
| 84 | + |
| 85 | + override def invokeAny[T](tasks: util.Collection[_ <: Callable[T]], timeout: Long, unit: TimeUnit): T = { |
| 86 | + executor.invokeAny(tasks, timeout, unit) |
| 87 | + } |
| 88 | + |
| 89 | + override def execute(command: Runnable): Unit = { |
| 90 | + executor.execute(command) |
| 91 | + } |
| 92 | +} |
0 commit comments