Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/schedulers/RxThreadFactory.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 io.reactivex.rxjava3.annotations.NonNull;
17
18 import java.util.concurrent.ThreadFactory;
19 import java.util.concurrent.atomic.AtomicLong;
20
21 /**
22 * A ThreadFactory that counts how many threads have been created and given a prefix,
23 * sets the created Thread's name to {@code prefix-count}.
24 */
25 public final class RxThreadFactory extends AtomicLong implements ThreadFactory {
26
27 private static final long serialVersionUID = -7789753024099756196L;
28
29 final String prefix;
30
31 final int priority;
32
33 final boolean nonBlocking;
34
35 // static volatile boolean CREATE_TRACE;
36
37 public RxThreadFactory(String prefix) {
38 this(prefix, Thread.NORM_PRIORITY, false);
39 }
40
41 public RxThreadFactory(String prefix, int priority) {
42 this(prefix, priority, false);
43 }
44
45 public RxThreadFactory(String prefix, int priority, boolean nonBlocking) {
46 this.prefix = prefix;
47 this.priority = priority;
48 this.nonBlocking = nonBlocking;
49 }
50
51 @Override
52 public Thread newThread(@NonNull Runnable r) {
53 StringBuilder nameBuilder = new StringBuilder(prefix).append('-').append(incrementAndGet());
54
55 // if (CREATE_TRACE) {
56 // nameBuilder.append("\r\n");
57 // for (StackTraceElement se :Thread.currentThread().getStackTrace()) {
58 // String s = se.toString();
59 // if (s.contains("sun.reflect.")) {
60 // continue;
61 // }
62 // if (s.contains("junit.runners.")) {
63 // continue;
64 // }
65 // if (s.contains("org.gradle.internal.")) {
66 // continue;
67 // }
68 // if (s.contains("java.util.concurrent.ThreadPoolExecutor")) {
69 // continue;
70 // }
71 // nameBuilder.append(s).append("\r\n");
72 // }
73 // }
74
75 String name = nameBuilder.toString();
76 Thread t = nonBlocking ? new RxCustomThread(r, name) : new Thread(r, name);
77 t.setPriority(priority);
78 t.setDaemon(true);
79 return t;
80 }
81
82 @Override
83 public String toString() {
84 return "RxThreadFactory[" + prefix + "]";
85 }
86
87 static final class RxCustomThread extends Thread implements NonBlockingThread {
88 RxCustomThread(Runnable run, String name) {
89 super(run, name);
90 }
91 }
92 }