I have one singleton object on which I would exactly one thread executing while the app is running.
So far I have created the thread in MainActiviy::onCreate
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
thread{myobject.run()}
}
}
But contrary to the documentation where the arrow leading to onCreate()
is App process is killed, it looks like onCreate()
is called every time the app is restarted regardless of if being killed or if its threads were still running.
object myobject{
fun run(){
while(true){
do_stuff()
}
}
}
It is of course possible to acquire a lock to only start the thread once, but since there is a nice syntax in Kotlin for singleton objects, and this is a related (I assume very common) problem I came to believe there maybe could be a simple more elegant way for this.
Or is the preferred way to acquire a lock on the object?