Skip to content

Instantly share code, notes, and snippets.

@devrath
Last active September 17, 2022 17:42
Show Gist options
  • Select an option

  • Save devrath/3fcea7aafc16f0e2bc84eca6e7458352 to your computer and use it in GitHub Desktop.

Select an option

Save devrath/3fcea7aafc16f0e2bc84eca6e7458352 to your computer and use it in GitHub Desktop.
This snippet contains code on how to use a class that implementing runnable class to achieve threading in android
class CustomRunnableOne : Runnable {
override fun run() {
try {
val TAG_ONE = "one"
for (i in 0 until 5) {
Thread.sleep(500L)
println(TAG_NARUTO.plus("--$i--").plus("Current Runnable-->$TAG_ONE"))
}
}catch (ex:InterruptedException){
println(TAG_NARUTO.plus("--")
.plus("Runnable named")
.plus("--")
.plus(CustomRunnableOne::class.java.name)
.plus("--")
.plus("is interrupted"))
}
}
}
class CustomRunnableThree : Runnable {
override fun run() {
try {
val TAG_THREE = "three"
for (i in 0 until 5) {
Thread.sleep(500L)
println(TAG_NARUTO.plus("--$i--").plus("Current Runnable-->$TAG_THREE"))
}
}catch (ex:InterruptedException){
println(TAG_NARUTO.plus("--")
.plus("Runnable named")
.plus("--")
.plus(CustomRunnableThree::class.java.name)
.plus("--")
.plus("is interrupted"))
}
}
}
class CustomRunnableTwo : Runnable {
override fun run() {
try {
val TAG_TWO = "two"
for (i in 0 until 5) {
Thread.sleep(500L)
println(TAG_NARUTO.plus("--$i--").plus("Current Runnable-->$TAG_TWO"))
}
}catch (ex:InterruptedException){
println(TAG_NARUTO.plus("--")
.plus("Runnable named")
.plus("--")
.plus(CustomRunnableTwo::class.java.name)
.plus("--")
.plus("is interrupted"))
}
}
}
2022-09-17 23:09:37.553 20681-20816/com.droid.code I/System.out: Naruto--0--Current Runnable-->one
2022-09-17 23:09:38.055 20681-20816/com.droid.code I/System.out: Naruto--1--Current Runnable-->one
2022-09-17 23:09:38.072 20681-20817/com.droid.code I/System.out: Naruto--0--Current Runnable-->two
2022-09-17 23:09:38.558 20681-20816/com.droid.code I/System.out: Naruto--2--Current Runnable-->one
2022-09-17 23:09:38.573 20681-20817/com.droid.code I/System.out: Naruto--1--Current Runnable-->two
2022-09-17 23:09:38.766 20681-20818/com.droid.code I/System.out: Naruto--0--Current Runnable-->three
2022-09-17 23:09:39.060 20681-20816/com.droid.code I/System.out: Naruto--3--Current Runnable-->one
2022-09-17 23:09:39.075 20681-20817/com.droid.code I/System.out: Naruto--2--Current Runnable-->two
2022-09-17 23:09:39.268 20681-20818/com.droid.code I/System.out: Naruto--1--Current Runnable-->three
2022-09-17 23:09:39.548 20681-20816/com.droid.code I/System.out: Naruto--Runnable named--com.droid.code.runnable.workers.CustomRunnableOne--is interrupted
2022-09-17 23:09:39.548 20681-20818/com.droid.code I/System.out: Naruto--Runnable named--com.droid.code.runnable.workers.CustomRunnableThree--is interrupted
2022-09-17 23:09:39.550 20681-20817/com.droid.code I/System.out: Naruto--Runnable named--com.droid.code.runnable.workers.CustomRunnableTwo--is interrupted
class RunnableActivity : AppCompatActivity() {
private lateinit var binding: ActivityRunnableBinding
lateinit var thread1 : Thread
lateinit var thread2 : Thread
lateinit var thread3 : Thread
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityRunnableBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btnPublishOneId.setOnClickListener {
val runnable = CustomRunnableOne()
thread1 = Thread(runnable)
thread1.name = CustomRunnableOne::javaClass.name
thread1.start()
}
binding.btnPublishTwoId.setOnClickListener {
val runnable = CustomRunnableTwo()
thread2 = Thread(runnable)
thread2.name = CustomRunnableTwo::javaClass.name
thread2.start()
}
binding.btnPublishThreeId.setOnClickListener {
val runnable = CustomRunnableThree()
thread3 = Thread(runnable)
thread3.name = CustomRunnableThree::javaClass.name
thread3.start()
}
binding.btnStopThreadId.setOnClickListener {
stopThreads()
}
}
private fun stopThreads() {
if(::thread1.isInitialized){
thread1.interrupt()
}
if(::thread2.isInitialized){
thread2.interrupt()
}
if(::thread3.isInitialized){
thread3.interrupt()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment