停止/杀死可运行线程-启动新的可运行线程-Java
我试图阻止Swing GUI中的可运行线程。当我点击按钮停止可运行线程时,它会停止它,但之后我无法启动新的可运行线程 有人知道这是为什么吗?任何帮助都将不胜感激 谢谢 这是我的GUI代码列表停止/杀死可运行线程-启动新的可运行线程-Java,java,multithreading,swing,selenium-webdriver,Java,Multithreading,Swing,Selenium Webdriver,我试图阻止Swing GUI中的可运行线程。当我点击按钮停止可运行线程时,它会停止它,但之后我无法启动新的可运行线程 有人知道这是为什么吗?任何帮助都将不胜感激 谢谢 这是我的GUI代码列表 if(button.getText().equals("Start Scraper")){ if(validate()) { updateThread.running = true; button.setText("St
if(button.getText().equals("Start Scraper")){
if(validate())
{
updateThread.running = true;
button.setText("Stop Scraper");
String searchType = comboBox.getSelectedItem().toString();
String email = emailTextField.getText();
String password = passwordTextField.getText();
String searchTerm = searchTermTextField.getText();
try{
thread = new updateThread(searchTerm, searchType, email, password );
thread.start();
}catch(Exception ex){
System.out.println("Something went wrong in the GUI");
ex.printStackTrace();
}
}else{
//not valid go again
}
}else{
button.setText("Start Crawler");
updateThread.running = false;
updateThread.terminate();
}
}
});
这是我的runnable线程类
package guiTool;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.SessionNotFoundException;
import linkedIncrawler.common.Utils;
import linkedin.actions.BaseClass;
import linkedin.actions.LinkedInActions;
public class updateThread extends Thread
{
private static WebDriver driver;
public String searchTerm, searchType, email, password;;
public volatile static Boolean running = true;
public updateThread(String searchTerm2, String searchType2, String email2, String password2)
{
email = email2;
password = password2;
searchTerm = searchTerm2;
searchType = searchType2;
}
public static void terminate() {
currentThread().interrupt();
//thread.stop();
driver.quit();
running = false;
}
@Override
public void run()
{
while(running)
{
try {
driver = Utils.OpenBrowser("SearchTerms");
new BaseClass(driver);
LinkedInActions.Execute(searchTerm, searchType, email, password);
driver.quit();
} catch (Exception e) {
System.out.println("2nd thread cant run linkedin");
e.printStackTrace();
}
}
}
}
一旦一根线死了,它就死了。。。你需要创建一个新的。有一些重要的原因可以解释为什么你不能重新启动一个死线程
与其扩展
线程
,不如实现可运行/可调用
?一旦线程死了,它就死了。。。你需要创建一个新的。有一些重要的原因可以解释为什么你不能重新启动一个死线程
public volatile static Boolean running = true;
与其扩展线程
,不如实现可运行/可调用
public volatile static Boolean running = true;
此变量对于所有实例都是通用的,它会停止所有updateThread、current和future。移除静态修改器
此变量对于所有实例都是通用的,它会停止所有updateThread、current和future。删除静态修饰符。我以为我是在第二次调用扩展线程类中的构造函数来创建一个新线程?@ControlAltDel为什么不?实际上,正如您所建议的,在thread=newupdatethread(searchTerm、searchType、email、password)行中创建了一个新线程;我以为我是在第二次调用扩展线程类中的构造函数来创建一个新线程?@ControlAltDel为什么不?实际上,正如您所建议的,在thread=newupdatethread(searchTerm、searchType、email、password)行中创建了一个新线程;