在Android上的Java的FOR循环中使用thread.start时,发生了threadthread.start已经启动的非法线程状态异常线程 [英] illegalthreadstateexception thread already started occurs when using thread.start in a FOR loop in Java on Android
问题描述
我正在使用一个FOR循环,该循环被设计为在每次迭代时再次启动线程.该线程用于从文本文件中提取数据.
I am using a FOR loop which has been designed to start the thread again on every iteration. The thread is used to extract data from text files.
该错误在第二次迭代中发生,错误为"illegalthreadstateexception线程已启动".我已经尝试过使用GetTopics.stop,即使它已被弃用,但尚未修复该错误.我还使用GetTopics.join来确保代码不会同时运行.
The error occurs on the second iteration where the error is "illegalthreadstateexception thread already started". I have tried using GetTopics.stop even though it has been deprecated but has not fixed the error. I also use GetTopics.join as a way of making sure the codes do not run simultaneously.
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或再次运行它,则需要创建一个新的线程对象并运行它.
First off- a thread cannot be run twice. You need to create a new thread object and run it if you want to run 2 in parallel or run it again.
其次-如果您执行了thread.start后立即执行thread.join,则该线程毫无意义.我意识到这可能只是调试代码,但如果不是,则您在这里遇到一些体系结构问题.
Secondly- if you do a thread.start immediately followed by a thread.join, the thread is pointless. I realize this may just have been debug code, but if it isn't you have some architecture problems here.
这篇关于在Android上的Java的FOR循环中使用thread.start时,发生了threadthread.start已经启动的非法线程状态异常线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!