作者:
Dong0129 (阿東跟上面講的情況一樣b)
2018-05-09 12:02:15各位版友好,
我利用thread處理文件內容,
但是執行後發現不會依照行數順序執行..
try
{
FileReader reader=new FileReader("test.txt");
BufferedReader br=new BufferedReader(reader);
String line;
while((line=br.readLine())!=null)
{
final String fline=line;
new Thread()
{
@Override
public void run()
{
String head=command_class(fline,0).toUpperCase();
//command_class會得到每行第一個元素
if(head.equals("TITLE"))
{
runOnUiThread(new Runnable() {
public void run() {
textview.setText(line);
}
});
}
else if(head.equals("WRITE"))
{
String str=command_class(fline,1);
//command_class會得到每行第二個元素
runOnUiThread(new Runnable() {
public void run() {
textview.setText(line);
}
});
}
else if(head.equals("SLEEP"))
{
//sleep.start();
try
{
Log.i(TAG,"Sleep");
Thread.currentThread().sleep(100000);
}catch(Exception e)
{
e.printStackTrace();
}
}
else if(head.equals("READ"))
{
runOnUiThread(new Runnable() {
public void run() {
textview.setText(line);
}
});
}
}
}.start();
}
}catch(Exception e)
{
e.printStackTrace();
}
執行後發現還沒睡完就寫了下一行..
請問為什麼會這樣呢?
該怎麼調整才會按照順序執行呢?