在Android上Java的FOR循环中使用thread.start时,出现illegalthreadstateexception线程已启动
我正在使用一个FOR循环,它被设计为在每次迭代时再次启动线程。线程用于从文本文件中提取数据 该错误发生在第二次迭代中,错误为illegalthreadstateexception线程已启动。我尝试过使用GetTopics.stop,尽管它已被弃用,但尚未修复错误。我还使用GetTopics.join来确保代码不会同时运行在Android上Java的FOR循环中使用thread.start时,出现illegalthreadstateexception线程已启动,java,android,multithreading,for-loop,Java,Android,Multithreading,For Loop,我正在使用一个FOR循环,它被设计为在每次迭代时再次启动线程。线程用于从文本文件中提取数据 该错误发生在第二次迭代中,错误为illegalthreadstateexception线程已启动。我尝试过使用GetTopics.stop,尽管它已被弃用,但尚未修复错误。我还使用GetTopics.join来确保代码不会同时运行 private void addTabs(ActionBar actionBar) { ActionBar.Tab tab1=actionBar.newTab();
private void addTabs(ActionBar actionBar)
{
ActionBar.Tab tab1=actionBar.newTab();
tab1.setText("All");
tab1.setTabListener(this);
actionBar.addTab(tab1);
for (addTabPosition = 2; addTabPosition < 11; addTabPosition++) {
Thread GetTopics = new Thread();
GetTopics.start();
try {
GetTopics.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(addTabMessage.contentEquals("FileNotFound")){
Log.e("skiper", "file skiped" + addTabPosition);
}else{
switch (addTabPosition) {
case 2:
ActionBar.Tab tab2=actionBar.newTab();
tab2.setText(addTabTitle);
tab2.setTabListener(this);
actionBar.addTab(tab2);
break;
case 3:
ActionBar.Tab tab3=actionBar.newTab();
tab3.setText(addTabTitle);
tab3.setTabListener(this);
actionBar.addTab(tab3);
break;
case 4:
ActionBar.Tab tab4=actionBar.newTab();
tab4.setText(addTabTitle);
tab4.setTabListener(this);
actionBar.addTab(tab4);
break;
case 5:
ActionBar.Tab tab5=actionBar.newTab();
tab5.setText(addTabTitle);
tab5.setTabListener(this);
actionBar.addTab(tab5);
break;
case 6:
ActionBar.Tab tab6=actionBar.newTab();
tab6.setText(addTabTitle);
tab6.setTabListener(this);
actionBar.addTab(tab6);
break;
case 7:
ActionBar.Tab tab7=actionBar.newTab();
tab7.setText(addTabTitle);
tab7.setTabListener(this);
actionBar.addTab(tab7);
break;
case 8:
ActionBar.Tab tab8=actionBar.newTab();
tab8.setText(addTabTitle);
tab8.setTabListener(this);
actionBar.addTab(tab8);
break;
case 9:
ActionBar.Tab tab9=actionBar.newTab();
tab9.setText(addTabTitle);
tab9.setTabListener(this);
actionBar.addTab(tab9);
break;
case 10:
ActionBar.Tab tab10=actionBar.newTab();
tab10.setText(addTabTitle);
tab10.setTabListener(this);
actionBar.addTab(tab10);
break;
}
}
}
}
一个线程不能运行两次。如果要并行运行2或再次运行,则需要创建一个新的线程对象并运行它
第二,如果在执行thread.start之后紧接着执行thread.join,则该线程是没有意义的。我意识到这可能只是调试代码,但如果不是,您在这里会遇到一些架构问题。您能否提供一个创建新线程对象的示例,因为我确实想再次运行它。我在FOR循环的开头添加了这行代码。不过,我看不到任何改善。