【发布时间】:2020-12-11 17:07:49
【问题描述】:
我在 Stackoverflow 上找到了多个执行此操作的选项,但仍然没有找到使用参数执行此操作的好方法。就我而言,我有一个线程需要不断更新 UI 主线程的日志信息(我没有使用 Asynctask;我正在从 Google Maps 实现 TileProvider 接口)
这就是我在我的线程中运行的东西,它可以工作:
Handler mainHandler = new Handler(Looper.getMainLooper());
...
public void debugMsg(final String msg)
{
Runnable myRunnable = (new Runnable()
{
@Override
public void run()
{
Log.Add(msg); // static function that update the UI in the main thread
}
});
mainHandler.post(myRunnable);
}
问题是如果我理解正确的话,每次我调用debugMsg,都会实例化一个新的Runnable对象。我不认为这是一个很好的做法,特别是因为它会被大量完成,所以我想知道是否有办法在我的线程中重用该可运行文件。或者也许有更好的方法......
【问题讨论】:
标签: java android multithreading user-interface