He-Pin commented on code in PR #1299: URL: https://github.com/apache/pekko/pull/1299#discussion_r1582044771
########## actor/src/main/scala/org/apache/pekko/dispatch/NewVirtualThreadPerTaskExecutor.scala: ########## @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pekko.dispatch + +import java.util +import java.util.Collections +import java.util.concurrent._ +import java.util.concurrent.atomic.AtomicInteger +import java.util.concurrent.locks.ReentrantLock + +private[dispatch] class NewVirtualThreadPerTaskExecutor(threadFactory: ThreadFactory) extends AbstractExecutorService { + import NewVirtualThreadPerTaskExecutor._ + + /** + * 0 RUNNING + * 1 SHUTDOWN + * 2 TERMINATED + */ + private val state = new AtomicInteger(RUNNING) + private val virtualThreads = ConcurrentHashMap.newKeySet[Thread]() + private val terminateLock = new ReentrantLock() + private val terminatedCondition = terminateLock.newCondition() + + override def shutdown(): Unit = { + shutdown(false) + } + + private def shutdown(interrupt: Boolean): Unit = { + if (!isShutdown) { + terminateLock.lock() + try { + if (isTerminated) { + () + } else { + if (state.compareAndSet(RUNNING, SHUTDOWN) && interrupt) { + virtualThreads.forEach(thread => { + if (!thread.isInterrupted) { + thread.interrupt() + } + }) + } + tryTerminateAndSignal() + } + } finally { + terminateLock.unlock() + } + } + } + + private def tryTerminateAndSignal(): Unit = { + if (isTerminated) { + () + } + terminateLock.lock() + try { + if (isTerminated) { + return + } + if (virtualThreads.isEmpty && state.compareAndSet(SHUTDOWN, TERMINATED)) { + terminatedCondition.signalAll() + } + } finally { + terminateLock.unlock() + } + } + + override def shutdownNow(): util.List[Runnable] = { + shutdown(true) + Collections.emptyList() + } + + override def isShutdown: Boolean = state.get() >= SHUTDOWN + + override def isTerminated: Boolean = state.get() >= TERMINATED + + private def isRunning: Boolean = state.get() == RUNNING + + override def awaitTermination(timeout: Long, unit: TimeUnit): Boolean = { + if (isTerminated) { + return true + } + terminateLock.lock() + try { + var nanosRemaining = unit.toNanos(timeout) + while (!isTerminated && nanosRemaining > 0) { + nanosRemaining = terminatedCondition.awaitNanos(nanosRemaining) + } + } finally { + terminateLock.unlock() + } + isTerminated + } + + // TODO AS only this execute method is been used in `Dispatcher.scala`, so `submit` and other methods is not override. + override def execute(command: Runnable): Unit = { + if (state.get() >= SHUTDOWN) { + throw new RejectedExecutionException("Shutdown") + } + var started = false; + try { + val thread = threadFactory.newThread(Task(this, command)) + virtualThreads.add(thread) Review Comment: Thanks, would you like to continue the work? I will continue this when find time. I have a isStarted check beblow, that would help? Another issue is, only execute method is been used, not sure what the origin design requires an ExecutionService. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
