Skip to content

Instantly share code, notes, and snippets.

@gordinmitya
Last active June 3, 2020 09:05
Show Gist options
  • Save gordinmitya/7b9fce77d9b5d9587891515a9422fad5 to your computer and use it in GitHub Desktop.
Save gordinmitya/7b9fce77d9b5d9587891515a9422fad5 to your computer and use it in GitHub Desktop.
Try to stop a Thread in Android

Approach with coroutines and withTimeout(timeout)

Result Log.d(TAG, "after while (true) {}") is never executed – there's no place to stop coroutine inside withTimeout(…)

"Либо аналогично с Котлином и коррутинами, если не возможно в цикле запускать suspend функции, чтобы были точки приостановки коррутины и запустить все это дело с помощью withTimeout(timeMillis: Long, block: suspend CoroutineScope.() -> T) метода"

2020-06-03 11:48:51.762 31766-31766/? I/e.myapplicatio: Late-enabling -Xcheck:jni
2020-06-03 11:48:51.788 31766-31766/? E/e.myapplicatio: Unknown bits set in runtime_flags: 0x8000
2020-06-03 11:48:51.961 31766-31766/com.example.myapplication I/Perf: Connecting to perf service.
2020-06-03 11:48:51.976 31766-31766/com.example.myapplication I/FeatureParser: can't find raphael.xml in assets/device_features/,it may be in /system/etc/device_features
2020-06-03 11:48:51.983 31766-31766/com.example.myapplication E/libc: Access denied finding property "ro.vendor.df.effect.conflict"
2020-06-03 11:48:51.969 31766-31766/com.example.myapplication W/e.myapplication: type=1400 audit(0.0:750211): avc: denied { read } for name="u:object_r:vendor_displayfeature_prop:s0" dev="tmpfs" ino=16677 scontext=u:r:untrusted_app:s0:c215,c257,c512,c768 tcontext=u:object_r:vendor_displayfeature_prop:s0 tclass=file permissive=0
2020-06-03 11:48:51.995 31766-31823/com.example.myapplication E/Perf: Fail to get file list com.example.myapplication
2020-06-03 11:48:51.995 31766-31823/com.example.myapplication E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-06-03 11:48:51.996 31766-31823/com.example.myapplication E/Perf: Fail to get file list com.example.myapplication
2020-06-03 11:48:51.996 31766-31823/com.example.myapplication E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-06-03 11:48:52.086 31766-31766/com.example.myapplication W/e.myapplicatio: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2020-06-03 11:48:52.086 31766-31766/com.example.myapplication W/e.myapplicatio: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2020-06-03 11:48:52.127 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:52.169 31766-31824/com.example.myapplication I/AdrenoGLES: QUALCOMM build : e0436c5, Ib165d46056
Build Date : 09/10/19
OpenGL ES Shader Compiler Version: EV031.27.05.01
Local Branch :
Remote Branch : quic/gfx-adreno.lnx.1.0.r74-rel
Remote Branch : NONE
Reconstruct Branch : NOTHING
2020-06-03 11:48:52.170 31766-31824/com.example.myapplication I/AdrenoGLES: Build Config : S P 8.0.11 AArch64
2020-06-03 11:48:52.172 31766-31824/com.example.myapplication I/AdrenoGLES: PFP: 0x016ee187, ME: 0x00000000
2020-06-03 11:48:52.173 31766-31824/com.example.myapplication W/AdrenoUtils: <ReadGpuID_from_sysfs:194>: Failed to open /sys/class/kgsl/kgsl-3d0/gpu_model
2020-06-03 11:48:52.173 31766-31824/com.example.myapplication W/AdrenoUtils: <ReadGpuID:218>: Failed to read chip ID from gpu_model. Fallback to use the GSL path
2020-06-03 11:48:52.169 31766-31766/com.example.myapplication W/RenderThread: type=1400 audit(0.0:750212): avc: denied { search } for name="kgsl" dev="sysfs" ino=55577 scontext=u:r:untrusted_app:s0:c215,c257,c512,c768 tcontext=u:object_r:sysfs_kgsl:s0 tclass=dir permissive=0
2020-06-03 11:48:52.186 31766-31824/com.example.myapplication W/Gralloc3: mapper 3.x is not supported
2020-06-03 11:48:52.377 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:52.617 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:52.828 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:53.032 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:53.236 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:53.440 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:53.847 31766-31833/com.example.myapplication I/chatty: uid=10471(com.example.myapplication) worker identical 2 lines
2020-06-03 11:48:54.051 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:54.255 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:55.501 31766-31833/com.example.myapplication I/chatty: uid=10471(com.example.myapplication) worker identical 6 lines
2020-06-03 11:48:55.710 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:55.919 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:56.129 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:56.338 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:56.544 31766-31833/com.example.myapplication I/chatty: uid=10471(com.example.myapplication) worker identical 1 line
2020-06-03 11:48:56.748 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:56.952 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:57.157 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:57.360 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:58.584 31766-31833/com.example.myapplication I/chatty: uid=10471(com.example.myapplication) worker identical 6 lines
2020-06-03 11:48:58.787 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:58.991 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:59.195 31766-31833/com.example.myapplication I/chatty: uid=10471(com.example.myapplication) worker identical 1 line
2020-06-03 11:48:59.399 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:59.603 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:59.812 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:48:59.964 31766-31822/com.example.myapplication I/e.myapplicatio: ProcessProfilingInfo new_methods=956 is saved saved_to_disk=1 resolve_classes_delay=8000
2020-06-03 11:49:00.022 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:00.231 31766-31833/com.example.myapplication I/chatty: uid=10471(com.example.myapplication) worker identical 1 line
2020-06-03 11:49:00.440 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:00.650 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:00.859 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:01.068 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:01.278 31766-31833/com.example.myapplication I/chatty: uid=10471(com.example.myapplication) worker identical 1 line
2020-06-03 11:49:01.487 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:01.703 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:02.122 31766-31833/com.example.myapplication I/chatty: uid=10471(com.example.myapplication) worker identical 2 lines
2020-06-03 11:49:02.332 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:02.541 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:03.561 31766-31833/com.example.myapplication I/chatty: uid=10471(com.example.myapplication) worker identical 5 lines
2020-06-03 11:49:03.765 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:03.969 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:06.824 31766-31833/com.example.myapplication I/chatty: uid=10471(com.example.myapplication) worker identical 14 lines
2020-06-03 11:49:07.028 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:07.232 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:07.436 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:07.640 31766-31833/com.example.myapplication I/chatty: uid=10471(com.example.myapplication) worker identical 1 line
2020-06-03 11:49:07.844 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:08.048 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:08.455 31766-31833/com.example.myapplication I/chatty: uid=10471(com.example.myapplication) worker identical 2 lines
2020-06-03 11:49:08.659 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:08.863 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:10.494 31766-31833/com.example.myapplication I/chatty: uid=10471(com.example.myapplication) worker identical 8 lines
2020-06-03 11:49:10.698 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:10.902 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:11.106 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:11.310 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:11.514 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:12.125 31766-31833/com.example.myapplication I/chatty: uid=10471(com.example.myapplication) worker identical 3 lines
2020-06-03 11:49:12.329 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:12.533 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:12.711 31766-31766/com.example.myapplication D/ViewRootImpl: [TouchInput][ViewRootImpl] KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x8, repeatCount=0, eventTime=1169418285, downTime=1169418285, deviceId=-1, source=0x101, displayId=-1 }
2020-06-03 11:49:12.714 31766-31766/com.example.myapplication D/ViewRootImpl: [TouchInput][ViewRootImpl] KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x8, repeatCount=0, eventTime=1169418285, downTime=1169418285, deviceId=-1, source=0x101, displayId=-1 }
2020-06-03 11:49:12.741 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:12.945 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:13.283 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:13.748 31766-31833/com.example.myapplication D/STOPME: tik-tok
2020-06-03 11:49:14.224 31766-31833/com.example.myapplication D/STOPME: tik-tok
package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.coroutines.*
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import kotlin.coroutines.CoroutineContext
class MainActivity : AppCompatActivity() {
val TAG = "STOPME"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
GlobalScope.launch(newSingleThreadContext("worker")) {
withTimeout(5_000) {
while (true) {
for (i in 1 until 100_100_500)
if (i == 1)
Log.d(TAG, "tik-tok")
}
}
Log.d(TAG, "after while (true) {}")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment