Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/schedulers/SchedulerPoolFactory.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.schedulers;
15
16 import java.util.concurrent.*;
17
18 import io.reactivex.rxjava3.exceptions.Exceptions;
19 import io.reactivex.rxjava3.functions.Function;
20
21 /**
22 * Manages the creating of ScheduledExecutorServices and sets up purging.
23 */
24 public final class SchedulerPoolFactory {
25 /** Utility class. */
26 private SchedulerPoolFactory() {
27 throw new IllegalStateException("No instances!");
28 }
29
30 static final String PURGE_ENABLED_KEY = "rx3.purge-enabled";
31
32 public static final boolean PURGE_ENABLED;
33
34 static {
35 SystemPropertyAccessor propertyAccessor = new SystemPropertyAccessor();
36 PURGE_ENABLED = getBooleanProperty(true, PURGE_ENABLED_KEY, true, true, propertyAccessor);
37 }
38
39 static boolean getBooleanProperty(boolean enabled, String key, boolean defaultNotFound, boolean defaultNotEnabled, Function<String, String> propertyAccessor) {
40 if (enabled) {
41 try {
42 String value = propertyAccessor.apply(key);
43 if (value == null) {
44 return defaultNotFound;
45 }
46 return "true".equals(value);
47 } catch (Throwable ex) {
48 Exceptions.throwIfFatal(ex);
49 return defaultNotFound;
50 }
51 }
52 return defaultNotEnabled;
53 }
54
55 static final class SystemPropertyAccessor implements Function<String, String> {
56 @Override
57 public String apply(String t) {
58 return System.getProperty(t);
59 }
60 }
61
62 /**
63 * Creates a ScheduledExecutorService with the given factory.
64 * @param factory the thread factory
65 * @return the ScheduledExecutorService
66 */
67 public static ScheduledExecutorService create(ThreadFactory factory) {
68 final ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1, factory);
69 exec.setRemoveOnCancelPolicy(PURGE_ENABLED);
70 return exec;
71 }
72 }