Converted NamedThreadFactory to kotlin

This commit is contained in:
Robinson 2022-03-05 21:57:42 +01:00
parent 811470d828
commit eddfbe7ec5
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C
2 changed files with 35 additions and 92 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2010 dorkbox, llc * Copyright 2022 dorkbox, llc
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,112 +13,56 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package dorkbox.util; package dorkbox.util
import java.util.concurrent.ThreadFactory; import java.util.concurrent.*
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.*
/** /**
* The default thread factory with names. * The default thread factory with names and daemon state
*/ */
public class NamedThreadFactory constructor(
class NamedThreadFactory implements ThreadFactory { /** @param namePrefix what you want the subsequent threads to be named. */
private final AtomicInteger poolId = new AtomicInteger(); val namePrefix: String,
public final ThreadGroup group; /** @param group the group this thread will belong to. If NULL, it will belong to the current thread group. */
public final String namePrefix; val group: ThreadGroup = Thread.currentThread().threadGroup,
public final int threadPriority;
public final boolean daemon;
/** /** @param threadPriority 1-10, with 5 being normal and 10 being max */
* Creates a DAEMON thread val threadPriority: Int = Thread.MAX_PRIORITY,
*/
public /** @param daemon true to stop this thread automatically when the JVM shutsdown */
NamedThreadFactory(String poolNamePrefix) { val daemon: Boolean = true
this(poolNamePrefix,
Thread.currentThread() ) : ThreadFactory {
.getThreadGroup(), constructor(poolNamePrefix: String, group: ThreadGroup) : this(poolNamePrefix, group, Thread.MAX_PRIORITY, true)
Thread.MAX_PRIORITY, constructor(poolNamePrefix: String, isDaemon: Boolean) : this(poolNamePrefix, Thread.currentThread().threadGroup, isDaemon)
true); constructor(poolNamePrefix: String, group: ThreadGroup, isDaemon: Boolean) : this(poolNamePrefix, group, Thread.MAX_PRIORITY, isDaemon)
private val poolId = AtomicInteger()
init {
require(threadPriority >= Thread.MIN_PRIORITY) {
String.format(
"Thread priority (%s) must be >= %s", threadPriority, Thread.MIN_PRIORITY
)
}
require(threadPriority <= Thread.MAX_PRIORITY) {
String.format(
"Thread priority (%s) must be <= %s", threadPriority, Thread.MAX_PRIORITY
)
}
} }
public override fun newThread(r: Runnable): Thread {
NamedThreadFactory(String poolNamePrefix, boolean isDaemon) {
this(poolNamePrefix,
Thread.currentThread()
.getThreadGroup(),
Thread.MAX_PRIORITY,
isDaemon);
}
/**
* Creates a DAEMON thread
*/
public
NamedThreadFactory(String poolNamePrefix, ThreadGroup group) {
this(poolNamePrefix, group, Thread.MAX_PRIORITY, true);
}
public
NamedThreadFactory(String poolNamePrefix, ThreadGroup group, boolean isDaemon) {
this(poolNamePrefix, group, Thread.MAX_PRIORITY, isDaemon);
}
/**
* Creates a DAEMON thread
*/
public
NamedThreadFactory(String poolNamePrefix, int threadPriority) {
this(poolNamePrefix, threadPriority, true);
}
public
NamedThreadFactory(String poolNamePrefix, int threadPriority, boolean isDaemon) {
this(poolNamePrefix, null, threadPriority, isDaemon);
}
/**
* @param poolNamePrefix what you want the subsequent threads to be named.
* @param group the group this thread will belong to. If NULL, it will belong to the current thread group.
* @param threadPriority 1-10, with 5 being normal and 10 being max
*/
public
NamedThreadFactory(String poolNamePrefix, ThreadGroup group, int threadPriority, boolean isDaemon) {
this.daemon = isDaemon;
this.namePrefix = poolNamePrefix;
if (group == null) {
this.group = Thread.currentThread()
.getThreadGroup();
}
else {
this.group = group;
}
if (threadPriority < Thread.MIN_PRIORITY) {
throw new IllegalArgumentException(String.format("Thread priority (%s) must be >= %s", threadPriority, Thread.MIN_PRIORITY));
}
if (threadPriority > Thread.MAX_PRIORITY) {
throw new IllegalArgumentException(String.format("Thread priority (%s) must be <= %s", threadPriority, Thread.MAX_PRIORITY));
}
this.threadPriority = threadPriority;
}
@Override
public
Thread newThread(Runnable r) {
// stack size is arbitrary based on JVM implementation. Default is 0 // stack size is arbitrary based on JVM implementation. Default is 0
// 8k is the size of the android stack. Depending on the version of android, this can either change, or will always be 8k // 8k is the size of the android stack. Depending on the version of android, this can either change, or will always be 8k
// To be honest, 8k is pretty reasonable for an asynchronous/event based system (32bit) or 16k (64bit) // To be honest, 8k is pretty reasonable for an asynchronous/event based system (32bit) or 16k (64bit)
// Setting the size MAY or MAY NOT have any effect!!! // Setting the size MAY or MAY NOT have any effect!!!
Thread t = new Thread(this.group, r, this.namePrefix + '-' + this.poolId.incrementAndGet()); val t = Thread(group, r, namePrefix + '-' + poolId.incrementAndGet())
t.setDaemon(this.daemon); t.isDaemon = daemon
if (t.getPriority() != this.threadPriority) { t.priority = threadPriority
t.setPriority(this.threadPriority); return t
}
return t;
} }
} }

View File

@ -120,8 +120,7 @@ class ParallelProcessor<Task> {
threads = new ArrayList<Thread>(numberOfThreads); threads = new ArrayList<Thread>(numberOfThreads);
ThreadGroup threadGroup = new ThreadGroup(Thread.currentThread() ThreadGroup threadGroup = new ThreadGroup(Thread.currentThread().getThreadGroup(), "ParallelProcessor");
.getThreadGroup(), "ParallelProcessor");
NamedThreadFactory dispatchThreadFactory = new NamedThreadFactory("Processor", threadGroup); NamedThreadFactory dispatchThreadFactory = new NamedThreadFactory("Processor", threadGroup);
for (int i = 0; i < numberOfThreads; i++) { for (int i = 0; i < numberOfThreads; i++) {
java.lang.Runnable runnable = new java.lang.Runnable() { java.lang.Runnable runnable = new java.lang.Runnable() {