Replace thread usages
MODIFY
docs/changelog.md
docs/changelog.md
@@ -478,11 +478,9 @@ class ReleaseExampleApplication : ExampleApplication() {
private val analysisExecutor by lazy {
Executors.newSingleThreadExecutor {
- Thread {
+ thread(start = false, name = "Heap analysis executor") {
android.os.Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND)
it.run()
- }.apply {
- name = "Heap analysis executor"
}
}
}

MODIFY
leakcanary-android-sample/src/main/java/com/example/leakcanary/MainActivity.kt
leakcanary-android-sample/src/main/java/com/example/leakcanary/MainActivity.kt
@@ -30,6 +30,7 @@ import android.os.SystemClock
import android.view.View
import android.widget.Button
import java.util.concurrent.atomic.AtomicReference
+import kotlin.concurrent.thread
import kotlin.random.Random
import leakcanary.AppWatcher
@@ -53,7 +54,7 @@ class MainActivity : Activity() {
2 -> {
// Leak from local variable on thread
val ref = AtomicReference(this)
- val thread = Thread {
+ thread(name = "Leaking local variables") {
val activity = ref.get()
ref.set(null)
while (true) {
@@ -61,8 +62,6 @@ class MainActivity : Activity() {
SystemClock.sleep(1000)
}
}
- thread.name = "Leaking local variables"
- thread.start()
}
// Leak from thread fields
else -> LeakingThread.thread.leakedViews.add(leakedView)

MODIFY
leakcanary-android-sample/src/release/java/com/example/leakcanary/ReleaseExampleApplication.kt
leakcanary-android-sample/src/release/java/com/example/leakcanary/ReleaseExampleApplication.kt
@@ -12,6 +12,7 @@ import leakcanary.ScreenOffTrigger
import shark.SharkLog
import shark.SharkLog.Logger
import java.util.concurrent.Executors
+import kotlin.concurrent.thread
class ReleaseExampleApplication : ExampleApplication() {
@@ -27,11 +28,9 @@ class ReleaseExampleApplication : ExampleApplication() {
)
val analysisExecutor = Executors.newSingleThreadExecutor {
- Thread {
+ thread(start = false, name = "Heap analysis executor") {
android.os.Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND)
it.run()
- }.apply {
- name = "Heap analysis executor"
}
}
analysisExecutor.execute {

MODIFY
shark-android/src/test/java/shark/HprofRetainedHeapPerfTest.kt
shark-android/src/test/java/shark/HprofRetainedHeapPerfTest.kt
@@ -4,6 +4,7 @@ import java.io.File
import java.util.EnumSet
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit.SECONDS
+import kotlin.concurrent.thread
import kotlin.math.absoluteValue
import kotlin.reflect.KClass
import org.assertj.core.api.AbstractIntegerAssert
@@ -154,12 +155,9 @@ class HprofRetainedHeapPerfTest {
): T {
lateinit var result: T
val latch = CountDownLatch(1)
- Thread {
+ thread(name = threadName) {
result = work()
latch.countDown()
- }.apply {
- name = threadName
- start()
}
check(latch.await(30, SECONDS))
return result

Replace thread usages