Skip to content

Instantly share code, notes, and snippets.

@lokeshsuryan
Created September 3, 2021 05:37
Show Gist options
  • Save lokeshsuryan/49e5a01dabbaf48ef9ef26f082df7187 to your computer and use it in GitHub Desktop.
Save lokeshsuryan/49e5a01dabbaf48ef9ef26f082df7187 to your computer and use it in GitHub Desktop.
package com.example.multithreaddemo
import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private var pi = 0.0
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
calculatePIValueBTn.setOnClickListener {
val startExecutionTime= System.currentTimeMillis()
val stringPI=stringFromJNI()
val endExecutionTime= System.currentTimeMillis()
val executionTime=endExecutionTime-startExecutionTime
sample_text.text="Acceleration kit exe time: $executionTime milli second value of PI $stringPI"
}
singleThreadExeBTn.setOnClickListener {
val startExecutionTime= System.currentTimeMillis()
pi=calculatePI(0, 1)
val endExecutionTime= System.currentTimeMillis()
val executionTime=endExecutionTime-startExecutionTime
sample_text.text="Single Thread execution time: $executionTime milli second value of PI $pi"
}
multipleThreadExeBTn.setOnClickListener {
val startExecutionTime= System.currentTimeMillis()
pi=calculatePIMultiThread()
val endExecutionTime= System.currentTimeMillis()
val executionTime=endExecutionTime-startExecutionTime
sample_text.text="Multiple Thread execution time: $executionTime milli second value of PI $pi"
}
// Example of a call to a native method
}
private fun calculatePI(initValue: Long, counter: Int) : Double {
val n: Long = 1000000000
var _pi = 0.0
var sign: Double
var j: Long = initValue
// pi calc formula: pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 ...
// pi calc formula: pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 ...
while (j <= n) {
sign = (if (j and 1 == 0L) -1 else 1).toDouble()
_pi += 4 * sign / (2 * j - 1)
j +=counter
}
return _pi
}
private fun calculatePIMultiThread(): Double {
var initValue = 0L
val maxThread = 8
val threads: MutableList<Thread> = ArrayList()
var thread: Thread
// Start threads ...
while (initValue < maxThread) {
thread = object : Thread() {
override fun run() {
val piValue: Double = calculatePI(initValue, maxThread)
pi += piValue
Log.d("dispatch", "Inside thread. calcPi = $piValue, mPi = $pi")
}
}
threads.add(thread)
thread.start()
initValue++
}
// Wait until all threads finish.
for (tt in threads) {
try {
tt.join()
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
return pi
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
external fun stringFromJNI(): String
companion object {
// Used to load the 'native-lib' library on application startup.
init {
System.loadLibrary("native-lib")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment