Skip to contentPackage: ExecutorWithPriority$2
ExecutorWithPriority$2
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.actor.impl;
28:
29: import javax.annotation.Nonnegative;
30: import javax.annotation.Nonnull;
31: import java.util.concurrent.BlockingQueue;
32: import java.util.concurrent.Executor;
33: import java.util.concurrent.LinkedBlockingDeque;
34: import java.util.concurrent.LinkedBlockingQueue;
35: import java.util.concurrent.ThreadFactory;
36: import java.util.concurrent.ThreadPoolExecutor;
37: import java.util.concurrent.TimeUnit;
38: import lombok.extern.slf4j.Slf4j;
39:
40: /***********************************************************************************************************************
41: *
42: * An executor that propagates the {@link it.tidalwave.actor.Collaboration}.
43: *
44: * @author Fabrizio Giudici
45: *
46: **********************************************************************************************************************/
47: @Slf4j
48: public class ExecutorWithPriority
49: {
50: private final Runnable consumer = new Runnable()
51: {
52: @Override
53: public void run()
54: {
55: for (;;)
56: {
57: final Runnable runnable = runnableQueue.poll();
58:
59: if (runnable == null)
60: {
61: break;
62: }
63:
64: runnable.run();
65: }
66: }
67: };
68:
69: private static interface PriorityRunnable extends Runnable
70: {
71: }
72:
73: private final BlockingQueue<Runnable> runnableQueue = new LinkedBlockingDeque<Runnable>()
74: {
75: @Override
76: public boolean add (@Nonnull final Runnable runnable)
77: {
78:• if (runnable instanceof PriorityRunnable)
79: {
80: addFirst(runnable);
81: }
82: else
83: {
84: addLast(runnable);
85: }
86:
87: return true;
88: }
89: };
90:
91: private final Executor executor;
92:
93: /*******************************************************************************************************************
94: *
95: * Creates an executor with the given pool size.
96: *
97: * @param poolSize the pool size
98: * @param name the thread base name
99: * @param initialPriority the initial thread priority in this executor
100: *
101: ******************************************************************************************************************/
102: public ExecutorWithPriority (@Nonnegative final int poolSize,
103: @Nonnull final String name,
104: @Nonnegative final int initialPriority)
105: {
106: final ThreadFactory threadFactory = new ThreadFactory()
107: {
108: private int count = 0;
109:
110: @Override @Nonnull
111: public Thread newThread (@Nonnull final Runnable runnable)
112: {
113: final Thread thread = new Thread(runnable, name + "-" + count++);
114: thread.setPriority(initialPriority);
115: return thread;
116: }
117: };
118:
119: // first parameter should be 0, but in this case it goes single thread
120: executor = new ThreadPoolExecutor(poolSize, poolSize, 2, TimeUnit.SECONDS,
121: new LinkedBlockingQueue<>(), threadFactory);
122: // executor = new ThreadPoolExecutor(poolSize, poolSize, 2, TimeUnit.SECONDS, runnableQueue, threadFactory);
123: }
124:
125: /*******************************************************************************************************************
126: *
127: * Schedules the execution of a worker at the end of the queue.
128: *
129: * @param worker the worker
130: *
131: ******************************************************************************************************************/
132: public void execute (@Nonnull final Runnable worker)
133: {
134: runnableQueue.add(worker);
135: executor.execute(consumer);
136: // executor.execute(worker);
137: }
138:
139: /*******************************************************************************************************************
140: *
141: * Schedules the execution of a worker as soon as possible.
142: *
143: * @param worker the worker
144: *
145: ******************************************************************************************************************/
146: public void executeWithPriority (@Nonnull final Runnable worker)
147: {
148: // executor.execute(new PriorityRunnable()
149: runnableQueue.add((PriorityRunnable)worker::run);
150: executor.execute(consumer);
151: }
152: }