Skip to content

Instantly share code, notes, and snippets.

@NizarETH
Last active November 23, 2022 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NizarETH/63fb18743a07b8b8f0cbf9b3cd88c6a1 to your computer and use it in GitHub Desktop.
Save NizarETH/63fb18743a07b8b8f0cbf9b3cd88c6a1 to your computer and use it in GitHub Desktop.
GlobalScope
coroutineScope
runBlocking
runBlocking 2
Job
Récupérer la valeur d'un Job
Exception
===============================
GlobalScope
===============================
Toast.makeText(this," before coroutine", Toast.LENGTH_LONG).show()
GlobalScope.launch {
delay(2000)
runOnUiThread {
Toast.makeText(this@MainActivity," inside coroutine", Toast.LENGTH_LONG).show()
}
}
Toast.makeText(this," after coroutine", Toast.LENGTH_LONG).show()
===============================
coroutineScope
===============================
Toast.makeText(this," before coroutine", Toast.LENGTH_LONG).show()
GlobalScope.launch {
coroutineScope { launch {
delay(2000)
runOnUiThread {
Toast.makeText(this@MainActivity," inside coroutine", Toast.LENGTH_LONG).show()
}
}
}
}
Toast.makeText(this," after coroutine", Toast.LENGTH_LONG).show()
===============================
runBlocking
===============================
Toast.makeText(this," before coroutine", Toast.LENGTH_LONG).show()
runBlocking (Dispatchers.Default){
delay(2000)
runOnUiThread {
Toast.makeText(this@MainActivity," inside coroutine", Toast.LENGTH_LONG).show()
}
}
Toast.makeText(this," after coroutine", Toast.LENGTH_LONG).show()
===============================
runBlocking 2
===============================
Toast.makeText(this," before coroutine", Toast.LENGTH_LONG).show()
runBlocking {
CoroutineScope(Dispatchers.Default).launch {
delay(2000)
runOnUiThread {
Toast.makeText(this@MainActivity," inside coroutine", Toast.LENGTH_LONG).show()
}
}
}
Toast.makeText(this," after coroutine", Toast.LENGTH_LONG).show()
===============================
Job
===============================
Toast.makeText(this," before coroutine", Toast.LENGTH_LONG).show()
runBlocking(Dispatchers.Default) {
val job = launch {
delay(2000)
runOnUiThread {
Toast.makeText(this@MainActivity," inside coroutine", Toast.LENGTH_LONG).show()
}
}
job.invokeOnCompletion {
runOnUiThread {
Toast.makeText(this@MainActivity," job completed", Toast.LENGTH_LONG).show()
}
}
delay(1000)
runOnUiThread {
Toast.makeText(this@MainActivity," job will be canceled", Toast.LENGTH_LONG).show()
}
job.cancel()
}
Toast.makeText(this," after coroutine", Toast.LENGTH_LONG).show()
==================================
Récupérer la valeur d'un Job
==================================
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Toast.makeText(this," before coroutine", Toast.LENGTH_LONG).show()
runBlocking(Dispatchers.Default) {
launch {
val value1 = async { getFirstValue() }
val value2 = async { getSecondeValue() }
val firstValue = value1.await()
val secondeValue = value2.await()
runOnUiThread()
{
Toast.makeText(this@MainActivity," first value " +firstValue +" seconde value "+secondeValue, Toast.LENGTH_LONG).show()
}
}
}
Toast.makeText(this," after coroutine", Toast.LENGTH_LONG).show()
}
suspend fun getFirstValue() : Int
{
return 2*3
}
suspend fun getSecondeValue() : Int
{
return 2*5
}
}
===============================
Exception
===============================
runBlocking {
try {
delay(2000)
} catch (e : Exception)
{
Toast.makeText(this@MainActivity,"test",Toast.LENGTH_LONG).show()
}
}
=============================
SupervisorJob
=============================
runBlocking {
launch(SupervisorJob()) {
delay(1000)
}
}
GlobalScope.launch() {
CoroutineScope(SupervisorJob()).launch {
delay(1000)
}
}
}
=============================
Flow
=============================
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
runBlocking {
sendPrimes().collect{
Toast.makeText(this@MainActivity, " number $it", Toast.LENGTH_LONG).show()
}
}
}
fun sendPrimes(): Flow<Int> = flow {
val primeList = listOf(2,7,8,9,1)
primeList.forEach{
delay(1000)
emit(it)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment