|
1 | 1 | package io.github.intisy.utils.concurrency; |
2 | 2 |
|
| 3 | +import java.util.function.BiConsumer; |
| 4 | +import java.util.function.BiFunction; |
| 5 | +import java.util.function.Consumer; |
| 6 | +import java.util.function.Function; |
| 7 | +import java.util.function.Supplier; |
| 8 | + |
3 | 9 | /** |
4 | 10 | * Utility class providing thread-related operations and convenience methods. |
5 | 11 | * This class includes methods for thread creation, sleep operations, and thread information retrieval. |
@@ -34,6 +40,235 @@ public static void sleep(int milliseconds) { |
34 | 40 | } |
35 | 41 | } |
36 | 42 |
|
| 43 | + /** |
| 44 | + * Creates and starts a new thread to execute the specified Consumer with the given input. |
| 45 | + * The thread will be created without a specific name and as a non-daemon thread. |
| 46 | + * |
| 47 | + * @param <T> the type of the input to the consumer |
| 48 | + * @param consumer the Consumer to be executed in the new thread |
| 49 | + * @param input the input to be passed to the consumer |
| 50 | + * @return the started Thread object |
| 51 | + */ |
| 52 | + public static <T> Thread newThread(Consumer<T> consumer, T input) { |
| 53 | + return newThread(() -> consumer.accept(input)); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates and starts a new thread with the specified name to execute the given Consumer with the input. |
| 58 | + * The thread will be created as a non-daemon thread. |
| 59 | + * |
| 60 | + * @param <T> the type of the input to the consumer |
| 61 | + * @param consumer the Consumer to be executed in the new thread |
| 62 | + * @param input the input to be passed to the consumer |
| 63 | + * @param name the name to assign to the new thread, or null for a default name |
| 64 | + * @return the started Thread object |
| 65 | + */ |
| 66 | + public static <T> Thread newThread(Consumer<T> consumer, T input, String name) { |
| 67 | + return newThread(() -> consumer.accept(input), name); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Creates and starts a new thread with the specified name and daemon status to execute the given Consumer with the input. |
| 72 | + * |
| 73 | + * @param <T> the type of the input to the consumer |
| 74 | + * @param consumer the Consumer to be executed in the new thread |
| 75 | + * @param input the input to be passed to the consumer |
| 76 | + * @param name the name to assign to the new thread, or null for a default name |
| 77 | + * @param daemon whether the thread should be a daemon thread |
| 78 | + * @return the started Thread object |
| 79 | + */ |
| 80 | + public static <T> Thread newThread(Consumer<T> consumer, T input, String name, boolean daemon) { |
| 81 | + return newThread(() -> consumer.accept(input), name, daemon); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Creates and starts a new thread to execute the specified BiConsumer with the given inputs. |
| 86 | + * The thread will be created without a specific name and as a non-daemon thread. |
| 87 | + * |
| 88 | + * @param <T> the type of the first input to the bi-consumer |
| 89 | + * @param <U> the type of the second input to the bi-consumer |
| 90 | + * @param biConsumer the BiConsumer to be executed in the new thread |
| 91 | + * @param input1 the first input to be passed to the bi-consumer |
| 92 | + * @param input2 the second input to be passed to the bi-consumer |
| 93 | + * @return the started Thread object |
| 94 | + */ |
| 95 | + public static <T, U> Thread newThread(BiConsumer<T, U> biConsumer, T input1, U input2) { |
| 96 | + return newThread(() -> biConsumer.accept(input1, input2)); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Creates and starts a new thread with the specified name to execute the given BiConsumer with the inputs. |
| 101 | + * The thread will be created as a non-daemon thread. |
| 102 | + * |
| 103 | + * @param <T> the type of the first input to the bi-consumer |
| 104 | + * @param <U> the type of the second input to the bi-consumer |
| 105 | + * @param biConsumer the BiConsumer to be executed in the new thread |
| 106 | + * @param input1 the first input to be passed to the bi-consumer |
| 107 | + * @param input2 the second input to be passed to the bi-consumer |
| 108 | + * @param name the name to assign to the new thread, or null for a default name |
| 109 | + * @return the started Thread object |
| 110 | + */ |
| 111 | + public static <T, U> Thread newThread(BiConsumer<T, U> biConsumer, T input1, U input2, String name) { |
| 112 | + return newThread(() -> biConsumer.accept(input1, input2), name); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Creates and starts a new thread with the specified name and daemon status to execute the given BiConsumer with the inputs. |
| 117 | + * |
| 118 | + * @param <T> the type of the first input to the bi-consumer |
| 119 | + * @param <U> the type of the second input to the bi-consumer |
| 120 | + * @param biConsumer the BiConsumer to be executed in the new thread |
| 121 | + * @param input1 the first input to be passed to the bi-consumer |
| 122 | + * @param input2 the second input to be passed to the bi-consumer |
| 123 | + * @param name the name to assign to the new thread, or null for a default name |
| 124 | + * @param daemon whether the thread should be a daemon thread |
| 125 | + * @return the started Thread object |
| 126 | + */ |
| 127 | + public static <T, U> Thread newThread(BiConsumer<T, U> biConsumer, T input1, U input2, String name, boolean daemon) { |
| 128 | + return newThread(() -> biConsumer.accept(input1, input2), name, daemon); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Creates and starts a new thread to execute the specified Supplier. |
| 133 | + * The thread will be created without a specific name and as a non-daemon thread. |
| 134 | + * Note: The return value of the Supplier is discarded. |
| 135 | + * |
| 136 | + * @param <T> the type of the result supplied by the supplier |
| 137 | + * @param supplier the Supplier to be executed in the new thread |
| 138 | + * @return the started Thread object |
| 139 | + */ |
| 140 | + public static <T> Thread newThread(Supplier<T> supplier) { |
| 141 | + return newThread(supplier::get); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Creates and starts a new thread with the specified name to execute the given Supplier. |
| 146 | + * The thread will be created as a non-daemon thread. |
| 147 | + * Note: The return value of the Supplier is discarded. |
| 148 | + * |
| 149 | + * @param <T> the type of the result supplied by the supplier |
| 150 | + * @param supplier the Supplier to be executed in the new thread |
| 151 | + * @param name the name to assign to the new thread, or null for a default name |
| 152 | + * @return the started Thread object |
| 153 | + */ |
| 154 | + public static <T> Thread newThread(Supplier<T> supplier, String name) { |
| 155 | + return newThread(supplier::get, name); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Creates and starts a new thread with the specified name and daemon status to execute the given Supplier. |
| 160 | + * Note: The return value of the Supplier is discarded. |
| 161 | + * |
| 162 | + * @param <T> the type of the result supplied by the supplier |
| 163 | + * @param supplier the Supplier to be executed in the new thread |
| 164 | + * @param name the name to assign to the new thread, or null for a default name |
| 165 | + * @param daemon whether the thread should be a daemon thread |
| 166 | + * @return the started Thread object |
| 167 | + */ |
| 168 | + public static <T> Thread newThread(Supplier<T> supplier, String name, boolean daemon) { |
| 169 | + return newThread(supplier::get, name, daemon); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Creates and starts a new thread to execute the specified Function with the given input. |
| 174 | + * The thread will be created without a specific name and as a non-daemon thread. |
| 175 | + * Note: The return value of the Function is discarded. |
| 176 | + * |
| 177 | + * @param <T> the type of the input to the function |
| 178 | + * @param <R> the type of the result of the function |
| 179 | + * @param function the Function to be executed in the new thread |
| 180 | + * @param input the input to be passed to the function |
| 181 | + * @return the started Thread object |
| 182 | + */ |
| 183 | + public static <T, R> Thread newThread(Function<T, R> function, T input) { |
| 184 | + return newThread(() -> function.apply(input)); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Creates and starts a new thread with the specified name to execute the given Function with the input. |
| 189 | + * The thread will be created as a non-daemon thread. |
| 190 | + * Note: The return value of the Function is discarded. |
| 191 | + * |
| 192 | + * @param <T> the type of the input to the function |
| 193 | + * @param <R> the type of the result of the function |
| 194 | + * @param function the Function to be executed in the new thread |
| 195 | + * @param input the input to be passed to the function |
| 196 | + * @param name the name to assign to the new thread, or null for a default name |
| 197 | + * @return the started Thread object |
| 198 | + */ |
| 199 | + public static <T, R> Thread newThread(Function<T, R> function, T input, String name) { |
| 200 | + return newThread(() -> function.apply(input), name); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Creates and starts a new thread with the specified name and daemon status to execute the given Function with the input. |
| 205 | + * Note: The return value of the Function is discarded. |
| 206 | + * |
| 207 | + * @param <T> the type of the input to the function |
| 208 | + * @param <R> the type of the result of the function |
| 209 | + * @param function the Function to be executed in the new thread |
| 210 | + * @param input the input to be passed to the function |
| 211 | + * @param name the name to assign to the new thread, or null for a default name |
| 212 | + * @param daemon whether the thread should be a daemon thread |
| 213 | + * @return the started Thread object |
| 214 | + */ |
| 215 | + public static <T, R> Thread newThread(Function<T, R> function, T input, String name, boolean daemon) { |
| 216 | + return newThread(() -> function.apply(input), name, daemon); |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Creates and starts a new thread to execute the specified BiFunction with the given inputs. |
| 221 | + * The thread will be created without a specific name and as a non-daemon thread. |
| 222 | + * Note: The return value of the BiFunction is discarded. |
| 223 | + * |
| 224 | + * @param <T> the type of the first input to the bi-function |
| 225 | + * @param <U> the type of the second input to the bi-function |
| 226 | + * @param <R> the type of the result of the bi-function |
| 227 | + * @param biFunction the BiFunction to be executed in the new thread |
| 228 | + * @param input1 the first input to be passed to the bi-function |
| 229 | + * @param input2 the second input to be passed to the bi-function |
| 230 | + * @return the started Thread object |
| 231 | + */ |
| 232 | + public static <T, U, R> Thread newThread(BiFunction<T, U, R> biFunction, T input1, U input2) { |
| 233 | + return newThread(() -> biFunction.apply(input1, input2)); |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * Creates and starts a new thread with the specified name to execute the given BiFunction with the inputs. |
| 238 | + * The thread will be created as a non-daemon thread. |
| 239 | + * Note: The return value of the BiFunction is discarded. |
| 240 | + * |
| 241 | + * @param <T> the type of the first input to the bi-function |
| 242 | + * @param <U> the type of the second input to the bi-function |
| 243 | + * @param <R> the type of the result of the bi-function |
| 244 | + * @param biFunction the BiFunction to be executed in the new thread |
| 245 | + * @param input1 the first input to be passed to the bi-function |
| 246 | + * @param input2 the second input to be passed to the bi-function |
| 247 | + * @param name the name to assign to the new thread, or null for a default name |
| 248 | + * @return the started Thread object |
| 249 | + */ |
| 250 | + public static <T, U, R> Thread newThread(BiFunction<T, U, R> biFunction, T input1, U input2, String name) { |
| 251 | + return newThread(() -> biFunction.apply(input1, input2), name); |
| 252 | + } |
| 253 | + |
| 254 | + /** |
| 255 | + * Creates and starts a new thread with the specified name and daemon status to execute the given BiFunction with the inputs. |
| 256 | + * Note: The return value of the BiFunction is discarded. |
| 257 | + * |
| 258 | + * @param <T> the type of the first input to the bi-function |
| 259 | + * @param <U> the type of the second input to the bi-function |
| 260 | + * @param <R> the type of the result of the bi-function |
| 261 | + * @param biFunction the BiFunction to be executed in the new thread |
| 262 | + * @param input1 the first input to be passed to the bi-function |
| 263 | + * @param input2 the second input to be passed to the bi-function |
| 264 | + * @param name the name to assign to the new thread, or null for a default name |
| 265 | + * @param daemon whether the thread should be a daemon thread |
| 266 | + * @return the started Thread object |
| 267 | + */ |
| 268 | + public static <T, U, R> Thread newThread(BiFunction<T, U, R> biFunction, T input1, U input2, String name, boolean daemon) { |
| 269 | + return newThread(() -> biFunction.apply(input1, input2), name, daemon); |
| 270 | + } |
| 271 | + |
37 | 272 | /** |
38 | 273 | * Creates and starts a new thread to run the specified Runnable. |
39 | 274 | * The thread will be created without a specific name and as a non-daemon thread. |
|
0 commit comments