Java BufferedReader检查readLine()是否会阻塞

时间:2019-01-24 14:38:06

标签: java android bufferedreader blocking inputstreamreader

我需要创建一个活动,该活动可以实时显示Android日志:

//In background thread:
Process process = Runtime.getRuntime().exec("logcat");
BufferedReader bufferedReader = new BufferedReader(
        new InputStreamReader(process.getInputStream()));

StringBuilder log=new StringBuilder();
String line = "";
while ((line = bufferedReader.readLine()) != null && !isInterrupted())
{
    log.append(line);
    mLoggerActivity.updateLog(log.toString()); 
}

...

public class LoggerActivity extends Activity
{
    private void updateLog(final String log) // Will be called from inner class
    {
        mUIThreadHadler.post
        (
            new Runnable()
            {
                @Override
                public void run()
                {
                    mLoggerView.setText(log);
                }
            }
        );
    }
...
}

我认为,每行更新UI效率不高-但是我还有其他选择吗?我需要一些诸如bufferedReader.canReadLine()的东西-因此我可以在将log.toString()发布到主线程之前批量更新StringBuilder。

0 个答案:

没有答案