# HG changeset patch # User autobackup # Date 1609600204 -32400 # Node ID 9404c0e15e397b4d3a2661558b4182b23276ad1d # Parent 96ca6a4bc184f2271f81236a06ae6d50e9434443 backup 2021-01-03 diff -r 96ca6a4bc184 -r 9404c0e15e39 Christie/for.NET.md --- a/Christie/for.NET.md Thu Dec 31 00:10:03 2020 +0900 +++ b/Christie/for.NET.md Sun Jan 03 00:10:04 2021 +0900 @@ -44,6 +44,11 @@ --- +## 実装を飛ばしている物(仮置きしているもの) +IncomingTcpConnetion 送られてくるデータを格納する配列のサイズ +PriorityThreadPoolExecutor ThreadPoolの優先度実装 + +--- ## Tips C# object → 全ての継承元クラス @@ -287,6 +292,16 @@ --- +優先度付きThreadPoolについて +javaのほうでは実装されているが、現状処理が複雑なため一旦実装を飛ばす +優先度が実装されている理由についてはheartbeatを実装したかったと考えられる + +Taskは内部でThreadPoolを作成しているらしい +https://oita.oika.me/2016/02/18/task-and-threadpool/ + +優先度をつけるならThreadPoolはTaskを利用できないかも + +--- ## TODO * annotation → fin * daemon @@ -298,11 +313,13 @@ * codegear * CodeGear → InputDataGear要実装 * InputDataGear - * DataGear周りができないと書け無さそう + * cgm要実装 * CodeGearManager * IncomingTcpConnection要実装 * ChristieDeamon要実装 * ThreadPoolExecutorとは + * CodeGearExecutor + * cgm要実装 * datagear * command * Command → fin diff -r 96ca6a4bc184 -r 9404c0e15e39 Christie/for.NET/優先度つきThreadPool.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Christie/for.NET/優先度つきThreadPool.md Sun Jan 03 00:10:04 2021 +0900 @@ -0,0 +1,184 @@ +## 元のコード + +```C# +package christie.codegear; + +import java.util.Comparator; +import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicLong; + + +// https://stackoverflow.com/questions/807223/how-do-i-implement-task-prioritization-using-an-executorservice-in-java-5/42831172#42831172 +public class PriorityThreadPoolExecutors { + + public static ThreadPoolExecutor createThreadPool(int nThreads, int keepAliveTime) { + return new PriorityThreadPoolExecutor(nThreads, nThreads, keepAliveTime, TimeUnit.MILLISECONDS); + } + private static class PriorityThreadPoolExecutor extends ThreadPoolExecutor { + private static final int DEFAULT_PRIORITY = 0; + private static AtomicLong instanceCounter = new AtomicLong(); + + public PriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, + int keepAliveTime, TimeUnit unit) { + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, (BlockingQueue) new PriorityBlockingQueue(10, + ComparableTask.comparatorByPriorityAndSequentialOrder())); + } + + @Override + public void execute(Runnable command) { + // If this is ugly then delegator pattern needed + if (command instanceof ComparableTask) //Already wrapped + super.execute(command); + else { + super.execute(newComparableRunnableFor(command)); + } + } + + private Runnable newComparableRunnableFor(Runnable runnable) { + return new ComparableRunnable((CodeGearExecutor) runnable); + } + + @Override + protected RunnableFuture newTaskFor(Runnable runnable, T value) { + return new ComparableFutureTask<>((CodeGearExecutor)runnable, value); + } + + private class ComparableFutureTask extends FutureTask implements ComparableTask { + private Long sequentialOrder = instanceCounter.getAndIncrement(); + private CodeGearExecutor hasPriority; + + public ComparableFutureTask(CodeGearExecutor priorityRunnable, T result) { + super(priorityRunnable, result); + this.hasPriority = priorityRunnable; + } + + @Override + public long getInstanceCount() { + return sequentialOrder; + } + + @Override + public int getPriority() { + return hasPriority.getPriority(); + } + } + + private static class ComparableRunnable implements Runnable, ComparableTask { + private Long instanceCount = instanceCounter.getAndIncrement(); + private CodeGearExecutor runnable; + + public ComparableRunnable(CodeGearExecutor priorityRunnable) { + this.runnable = priorityRunnable; + } + + @Override + public void run() { + runnable.run(); + } + + @Override + public int getPriority() { + return runnable.getPriority(); + } + + @Override + public long getInstanceCount() { + return instanceCount; + } + } + + private interface ComparableTask extends Runnable { + int getPriority(); + + long getInstanceCount(); + + static Comparator comparatorByPriorityAndSequentialOrder() { + return (o1, o2) -> { + int priorityResult = o2.getPriority() - o1.getPriority(); + return priorityResult != 0 ? priorityResult + : (int) (o1.getInstanceCount() - o2.getInstanceCount()); + }; + } + + } + + } + +} +``` + + + + +## 読み解き +参考元? +https://stackoverflow.com/questions/807223/how-do-i-implement-task-prioritization-using-an-executorservice-in-java-5/42831172#42831172 + +--- +```c# +public class PriorityThreadPoolExecutors { + + public static ThreadPoolExecutor createThreadPool(int nThreads, int keepAliveTime) { + return new PriorityThreadPoolExecutor(nThreads, nThreads, keepAliveTime, TimeUnit.MILLISECONDS); + } + private static class PriorityThreadPoolExecutor extends ThreadPoolExecutor { + private static final int DEFAULT_PRIORITY = 0; + private static AtomicLong instanceCounter = new AtomicLong(); + + public PriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, + int keepAliveTime, TimeUnit unit) { + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, (BlockingQueue) new PriorityBlockingQueue(10, + ComparableTask.comparatorByPriorityAndSequentialOrder())); + } +``` +ThreadPoolExecutorを継承したPriorityThreadPoolExecutorクラス +デフォルトの優先度は0 +スレッド数と生存時間を指定 +→スレッド数はcpu数によって変わる `Runtime.getRuntime().availableProcessors()` +https://stackoverflow.com/questions/11877947/runtime-getruntime-availableprocessors +c#だとこっち +https://oita.oika.me/2016/02/18/task-and-threadpool/ + + +生存時間はintの最大値が使用されている + +```c# +super(corePoolSize, maximumPoolSize, keepAliveTime, unit, (BlockingQueue) new PriorityBlockingQueue(10, + ComparableTask.comparatorByPriorityAndSequentialOrder())); +``` +この部分の`ComparableTask.comparatorByPriorityAndSequentialOrder()`はこの条件のもとでQueue内の入れ替えを行うという処理 +おそらく実行待ちのQueue内で入れ替えを行う + +--- +```c# +@Override +public void execute(Runnable command) { + // If this is ugly then delegator pattern needed + if (command instanceof ComparableTask) //Already wrapped + super.execute(command); + else { + super.execute(newComparableRunnableFor(command)); + } +} +``` +Runnableの実行部分 +ComparableTaskでラップすることが前提 +→ComparableTaskは優先度比較のクラス?? + +--- + +```c# + static Comparator comparatorByPriorityAndSequentialOrder() { + return (o1, o2) -> { + int priorityResult = o2.getPriority() - o1.getPriority(); + return priorityResult != 0 ? priorityResult + : (int) (o1.getInstanceCount() - o2.getInstanceCount()); + }; +} +``` +比較部分 +javaのComparatorはintを返すことで順序を比較する +→ retrunが o1とo2を比較して -1なら そのまま +→ 1なら入れ替える + +0の場合優先度の差がないので、instance生成順で並び替える diff -r 96ca6a4bc184 -r 9404c0e15e39 user/riono210.md --- a/user/riono210.md Thu Dec 31 00:10:03 2020 +0900 +++ b/user/riono210.md Sun Jan 03 00:10:04 2021 +0900 @@ -1,2 +1,4 @@ # riono210 -This is riono210's page \ No newline at end of file +This is riono210's page + +$lsx() \ No newline at end of file