To prevent IO from blocking the UI thread in my Android app, I am trying to move the file write operation to a separate thread. This is the code I use to start a low priority stream to write about 1 MB from the byte buffer:
Thread t = new Thread(new Runnable()
{
@Override
public void run()
{
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
try
{
FileOutputStream fos = new FileOutputStream(filename));
try
{
final java.nio.channels.FileChannel outChannel = fos.getChannel();
outChannel.write(byteBuffer);
fos.getFD().sync();
}
finally
{
if (fos != null)
fos.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
t.setPriority(Thread.MIN_PRIORITY);
t.start();
These threads are triggered in my application when certain user interface events occur. Unfortunately, I still notice a huge delay. My user interface will freeze for ~ 2 seconds every 10 or so times when I start / end one of the above threads. If I comment on the stream code, these delays will disappear.
, - ?
, , . , IO , "outChannel.write" , -, , . ?
Edit:
StrictView ( , - , ), , IO -.
traceview . , , 10 , , , 0,5-1 . , , , , . , . , traceview, , 1 , , , x10. , .drawBitmap( ) ~ 0.2s , .drawBitamp . , , .