简体   繁体   中英

Stop/Kill Runnable Thread - Start new Runnable thread - Java

Im trying trying to stop a runnable thread from a 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

    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 ?

public volatile static Boolean running = true;

this variable, being common for all instances, stops all updateThread's, current and future. Remove static modifier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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