diff --git a/mobile/src/main/java/com/github/shadowsocks/bg/GuardedProcess.kt b/mobile/src/main/java/com/github/shadowsocks/bg/GuardedProcess.kt index 46a3bfb3..a4faeb6a 100644 --- a/mobile/src/main/java/com/github/shadowsocks/bg/GuardedProcess.kt +++ b/mobile/src/main/java/com/github/shadowsocks/bg/GuardedProcess.kt @@ -27,13 +27,14 @@ import com.github.shadowsocks.App.Companion.app import com.github.shadowsocks.BuildConfig import com.github.shadowsocks.JniHelper import com.github.shadowsocks.utils.Commandline +import com.github.shadowsocks.utils.thread import java.io.IOException import java.io.InputStream import java.util.concurrent.Semaphore class GuardedProcess(private val cmd: List) { companion object { - const val TAG = "GuardedProcess" + private const val TAG = "GuardedProcess" } private lateinit var guardThread: Thread @@ -42,7 +43,7 @@ class GuardedProcess(private val cmd: List) { @Volatile private lateinit var process: Process - private fun streamLogger(input: InputStream, logger: (String, String) -> Int) = Thread { + private fun streamLogger(input: InputStream, logger: (String, String) -> Int) = thread { try { input.bufferedReader().useLines { it.forEach { logger(TAG, it) } } } catch (_: IOException) { } // ignore @@ -52,7 +53,7 @@ class GuardedProcess(private val cmd: List) { val semaphore = Semaphore(1) semaphore.acquire() var ioException: IOException? = null - guardThread = Thread({ + guardThread = thread(name = "GuardThread-" + cmd.first()) { try { var callback: (() -> Unit)? = null while (!isDestroyed) { @@ -64,8 +65,8 @@ class GuardedProcess(private val cmd: List) { .directory(app.filesDir) .start() - streamLogger(process.inputStream, Log::i).start() - streamLogger(process.errorStream, Log::e).start() + streamLogger(process.inputStream, Log::i) + streamLogger(process.errorStream, Log::e) if (callback == null) callback = onRestartCallback else callback() @@ -87,8 +88,7 @@ class GuardedProcess(private val cmd: List) { } finally { semaphore.release() } - }, "GuardThread-" + cmd.first()) - guardThread.start() + } semaphore.acquire() if (ioException != null) throw ioException!! return this