jt2594838 commented on code in PR #9667: URL: https://github.com/apache/iotdb/pull/9667#discussion_r1199874110
########## node-commons/src/main/java/org/apache/iotdb/commons/concurrent/pipeline/TaskRunner.java: ########## @@ -0,0 +1,82 @@ +/* + * 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.iotdb.commons.concurrent.pipeline; + +import org.apache.iotdb.commons.concurrent.dynamic.DynamicThread; +import org.apache.iotdb.commons.concurrent.dynamic.DynamicThreadGroup; + +import java.util.concurrent.BlockingQueue; + +/** + * A thread that will continuously take tasks from an input queue, run the task, and insert the next + * task to the output queue. By connecting multiple TaskRunners with input queues and output queues, + * a pipeline is formed. + */ +public class TaskRunner extends DynamicThread { + + private Runnable cleanUp; + private BlockingQueue<Task> input; + private BlockingQueue<Task> output; + private String taskName; + + public TaskRunner( + DynamicThreadGroup threadGroup, + Runnable cleanUp, + BlockingQueue<Task> input, + BlockingQueue<Task> output, + String taskName) { + super(threadGroup); + this.cleanUp = cleanUp; + this.input = input; + this.output = output; + this.taskName = taskName; + } + + @Override + public void runInternal() { + String baseName = Thread.currentThread().getName(); + Thread.currentThread().setName(baseName + "-" + taskName); + try { + while (!Thread.interrupted()) { + Task task; + try { + task = input.take(); + idleToRunning(); + task.run(); + Task nextTask = task.nextTask(); + if (nextTask != null) { + output.put(nextTask); + } + runningToIdle(); + + if (shouldExit()) { + break; + } + } catch (InterruptedException e1) { + Thread.currentThread().interrupt(); + break; + } + } + + cleanUp.run(); + } finally { + Thread.currentThread().setName(baseName); + } + } +} Review Comment: It is a good idea. However, the main intention of DynamicThread is to reduce the thread contention instead of the number of sleeping threads. The cost of contention is more significant as it consumes CPU resources to switch threads, while the sleeping threads do not consume any CPU resources. That is, if the queue is exhausted, the thread will fall asleep permanently until the main task cancels it, which is more affordable than a living thread competing with others. More importantly, it is very difficult to record the speed accurately and respond to it. Due to the existence of thread being switched out and garbage collection, the speed is not likely to be stable. The judgment of keeping up or not may be inaccurate or even erroneous, causing unwanted thread creation and destruction. If you have any idea that may precisely record and compare speed across tasks, I will be more than happy to make the improvement. -- 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]
