TimerTask vs Thread.sleep vs Handler postDelayed - most accurate to call function every N milliseconds?

后端 未结 4 1115
你的背包
你的背包 2020-11-28 22:47

What is the most accurate way to call a function every N milliseconds?

  • Thread with Thread.sleep
  • TimerTask
  • Handler with postDelayed
4条回答
  •  误落风尘
    2020-11-28 23:33

    On Android you can create Thread with it's own Handler/Message Queue. It's quite accurate. When you see Handler documentation you can see, that it was designed for that.

    There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

提交回复
热议问题