简体   繁体   English

停止/杀死可运行线程-启动新的可运行线程-Java

[英]Stop/Kill Runnable Thread - Start new Runnable thread - Java

Im trying trying to stop a runnable thread from a Swing GUI. 我试图阻止Swing GUI中的可运行线程。 When I click on the button to stop the runnable thread it stops it but I am unable to start a new runnable thread afterwards. 当我单击按钮以停止可运行线程时,它将停止它,但是此后我无法启动新的可运行线程。

Does anyone know why this is? 有人知道为什么是这样吗? Any help would be appreciated. 任何帮助,将不胜感激。

Thanks 谢谢

Here's my GUI Code Listnere 这是我的GUI Code Listnere

    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();

    }
    }
});

Here's my runnable thread class 这是我的可运行线程类

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();
                }
            }
   }

}

Once a thread has died, it is dead... You need to create a new one. 线程死亡后,它就死了……您需要创建一个新线程。 There are important reasons as to why you can't re-start a dead thread. 为什么不能重新启动死线程有重要的原因。

Rather than extending Thread , maybe implement Runnable/Callable ? 而不是扩展Thread ,可能实现Runnable/Callable吗?

public volatile static Boolean running = true;

this variable, being common for all instances, stops all updateThread's, current and future. 此变量在所有实例中都是通用的,它将停止当前和将来的所有updateThread。 Remove static modifier. 删除静态修饰符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM