[问题] Thread执行时的问题

楼主: 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();
}
执行后发现还没睡完就写了下一行..
请问为什么会这样呢?
该怎么调整才会按照顺序执行呢?
作者: ssccg (23)   2018-05-09 13:01:00
多条thread本来就不会照顺序执行,你要依序执行为什么不用single thread就好?把new Thread移到最外面,整段都在一个thread里做才对吧
作者: pass78   2018-05-20 22:46:00
可以多执行绪照顺序,用reactor 2的work queue event bus

Links booklink

Contact Us: admin [ a t ] ucptt.com