Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Api
- import kotlinx.coroutines.*
- import kotlinx.coroutines.channels.Channel
- import okhttp3.OkHttpClient
- import retrofit2.Retrofit
- import retrofit2.converter.simplexml.SimpleXmlConverterFactory
- import retrofit2.create
- fun main(args: Array<String>): Unit = runBlocking{
- // val channel = Channel<Int>()
- val threads = mutableListOf<Thread>()
- val service = Retrofit.Builder()
- .baseUrl("https://google.com/")
- .addConverterFactory(SimpleXmlConverterFactory.create())
- .client(OkHttpClient.Builder().build())
- .build()
- .create<Api>()
- coroutineScope {
- launch(Dispatchers.Default) {
- val thread = Thread.currentThread()
- thread.name = "${thread.name} <- THREAD 1"
- threads.add(thread)
- delay(1000)
- }
- launch(Dispatchers.Default) {
- val duration = System.currentTimeMillis() + 2000
- val thread = Thread.currentThread()
- thread.name = "${thread.name} <- THREAD 2"
- threads.add(thread)
- while (duration > System.currentTimeMillis()) { }
- }
- launch(Dispatchers.IO) {
- val thread = Thread.currentThread()
- thread.name = "${thread.name} <- RETROFIT"
- threads.add(thread)
- try {
- service.longResponse().execute() // без suspend
- } catch (e: Exception) {
- println(e.message)
- }
- }
- launch {
- val duration = System.currentTimeMillis() + 2000
- while (duration > System.currentTimeMillis()) {
- println()
- threads.forEach {
- println("${it.name}, state=${it.state}")
- }
- delay(100)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement