我正在尝试从 Swing GUI 停止可运行线程。当我单击按钮停止可运行线程时,它会停止它,但之后我无法启动新的可运行线程。
有谁知道这是为什么吗?任何帮助,将不胜感激。
谢谢
这是我的 GUI 代码列表
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();
}
}
});
这是我的可运行线程类
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();
}
}
}
}
最佳答案
一旦一个线程死了,它就死了......你需要创建一个新的线程。有一些重要原因导致您无法重新启动死线程。
与其扩展Thread
,不如实现Runnable/Callable
?
关于java - 停止/终止可运行线程 - 启动新的可运行线程 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34250907/