What would be the kotlin version of this? Runnabl...
# android
d
What would be the kotlin version of this? Runnable r = new Runnable() { @Override public void run() { while (mStatus) { synchronized (this) { try { wait(1000 * 60); } catch (Exception e) { e.printStackTrace(); continue; } } sendRequest(); } } }; thread = new Thread(r); thread.start();
i
I think you can copy and paste that in the IDE and it will convert it for you into Kotlin
d
when I use convert, "wait" is not usable
continue is not usable
j
Copy code
thread(start = true) {
      while(mStatus) {
        synchronized(this) {
          Thread.sleep(60_000)
          sendRequest()
        }
      }
    }
Allow the interrupt exception to propagate up?
d
It cause hang app
j
If you really need to call
wait
you can cast to java.lang.Object.
You can also return a value from
synchronised
Copy code
while(mStatus) {
  val shouldSendRequest = synchronized(this) {
    try {
      (this as java.lang.Object).wait(60_000)
      true
    } catch (ex: Exception) {
      false
    }
  }

  if (shouldSendRequest) sendRequest()
}
d
I will test it and say the result
o
you can also use coroutines , i assume
d
@James The whole problem was from my code. I was using "thread.run()" for run created Thread. Thanks a lot
@oianmol Isn't coroutines hard to learn? and isn't it is experimental stage?
o
@dr.henry its easy very easy , also experimental is temporary , If you dont want to use coroutines then you can use RxJAVA it has a lot of operators for the use case.
d
Do you can give me a good reference to learn coroutines?
d
Try the kotlinx.coroutines repo, we're using it, and it's much less mess than threads and runners, and we're also switching from rx....
d
Thanks a lot