Last active
September 17, 2022 16:28
-
-
Save devrath/215937fa39bea01ba0b438a3c82f762f to your computer and use it in GitHub Desktop.
This snippet contains code on how to use a class that extends thread class to achieve threading in android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CustomThreadOne : Thread() { | |
| override fun run() { | |
| try { | |
| val TAG_ONE = "one" | |
| for (i in 0 until 5) { | |
| Thread.sleep(500L) | |
| println(TAG_SASUKE.plus("--$i--").plus(name).plus("--").plus(TAG_ONE)) | |
| } | |
| }catch (ex:InterruptedException){ | |
| println(TAG_SASUKE.plus("--").plus("Thread named").plus("--").plus(name).plus("--").plus("is interrupted")) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CustomThreadThree : Thread() { | |
| override fun run() { | |
| try { | |
| val TAG_THREE = "three" | |
| for (i in 0 until 5) { | |
| Thread.sleep(500L) | |
| println(TAG_SASUKE.plus("--$i--").plus(name).plus("--").plus(TAG_THREE)) | |
| } | |
| }catch (ex:InterruptedException){ | |
| println(TAG_SASUKE.plus("--").plus("Thread named").plus("--").plus(name).plus("--").plus("is interrupted")) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CustomThreadTwo : Thread() { | |
| override fun run() { | |
| try { | |
| val TAG_TWO = "two" | |
| for (i in 0 until 5) { | |
| Thread.sleep(500L) | |
| println(TAG_SASUKE.plus("--$i--").plus(name).plus("--").plus(TAG_TWO)) | |
| } | |
| }catch (ex:InterruptedException){ | |
| println(TAG_SASUKE.plus("--").plus("Thread named").plus("--").plus(name).plus("--").plus("is interrupted")) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 2022-09-17 21:57:39.072 19401-19893/com.droid.code I/System.out: Sasuke--0--com.droid.code.thread.workers.CustomThreadOne--one | |
| 2022-09-17 21:57:39.562 19401-19894/com.droid.code I/System.out: Sasuke--0--com.droid.code.thread.workers.CustomThreadTwo--two | |
| 2022-09-17 21:57:39.574 19401-19893/com.droid.code I/System.out: Sasuke--1--com.droid.code.thread.workers.CustomThreadOne--one | |
| 2022-09-17 21:57:40.064 19401-19894/com.droid.code I/System.out: Sasuke--1--com.droid.code.thread.workers.CustomThreadTwo--two | |
| 2022-09-17 21:57:40.076 19401-19893/com.droid.code I/System.out: Sasuke--2--com.droid.code.thread.workers.CustomThreadOne--one | |
| 2022-09-17 21:57:40.219 19401-19895/com.droid.code I/System.out: Sasuke--0--com.droid.code.thread.workers.CustomThreadThree--three | |
| 2022-09-17 21:57:40.566 19401-19894/com.droid.code I/System.out: Sasuke--2--com.droid.code.thread.workers.CustomThreadTwo--two | |
| 2022-09-17 21:57:40.578 19401-19893/com.droid.code I/System.out: Sasuke--Thread named--com.droid.code.thread.workers.CustomThreadOne--is interrupted | |
| 2022-09-17 21:57:40.578 19401-19895/com.droid.code I/System.out: Sasuke--Thread named--com.droid.code.thread.workers.CustomThreadThree--is interrupted | |
| 2022-09-17 21:57:40.579 19401-19894/com.droid.code I/System.out: Sasuke--Thread named--com.droid.code.thread.workers.CustomThreadTwo--is interrupted |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ThreadActivity : AppCompatActivity() { | |
| private lateinit var binding: ActivityThreadBinding | |
| lateinit var threadOne : CustomThreadOne | |
| lateinit var threadTwo : CustomThreadTwo | |
| lateinit var threadThree : CustomThreadThree | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| binding = ActivityThreadBinding.inflate(layoutInflater) | |
| setContentView(binding.root) | |
| binding.btnPublishOneId.setOnClickListener { | |
| threadOne = CustomThreadOne() | |
| threadOne.name = CustomThreadOne::class.java.name; | |
| threadOne.start() | |
| } | |
| binding.btnPublishTwoId.setOnClickListener { | |
| threadTwo = CustomThreadTwo() | |
| threadTwo.name = CustomThreadTwo::class.java.name; | |
| threadTwo.start() | |
| } | |
| binding.btnPublishThreeId.setOnClickListener { | |
| threadThree = CustomThreadThree() | |
| threadThree.name = CustomThreadThree::class.java.name; | |
| threadThree.start() | |
| } | |
| binding.btnStopThreadId.setOnClickListener { stopThreads() } | |
| } | |
| private fun stopThreads() { | |
| if(::threadOne.isInitialized){ | |
| threadOne.interrupt() | |
| } | |
| if(::threadTwo.isInitialized){ | |
| threadTwo.interrupt() | |
| } | |
| if(::threadThree.isInitialized){ | |
| threadThree.interrupt() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment