Stop the running process through the GUI in Java

I have a GUI program that executes TestNG automation scripts This means that users can easily configure certain settings and start the automation scripts they want

One thing I need to add is the ability to immediately stop the running TestNG process As in eclipse, the terminate button immediately stops anything running

This is the code to start the TestNG test, as follows:

public class ScriptRunner implements Runnable {

    public void runScript() {
        Thread testRun = new Thread(this);
        testRun.start();
    }

    @Override
    public void run() {
        //varIoUs other things are configured for this,//but they're not relevant so I left them out
        TestNG tng = new TestNG();

        //While this runs,varIoUs browser windows are open,//and it Could take several minutes for it all to finish
        tng.run();
    }
}

According to the comments, TNG Run () may take a few minutes to complete. It is performing several operations, opening / closing browser windows, etc

How can I terminate the process immediately, just like running an application from the IDE?

Edit:

According to the comments, I tried to use serviceexecutor and shutdownnow(). The code looks like this:

ExecutorService executorService = Executors.newFixedThreadPool(10);

public void runScript() {
    executorService.execute(this);
}

//this method gets called when I click the "stop" button
public void stopRun() {
    executorService.shutdownNow();
}

@Override
public void run() {
    //same stuff as from earlier code
}

Solution

I've been working on the executive framework recently I have listed my problems here

If you are performing some IO operations, be careful. The program service may not shut down immediately If you see the following code, stopthread is important because it tells your program that it has asked the thread to stop You can stop some iterations of what you are doing I'll modify your code like this:

public class MyClass {

private ExecutorService executorService;

private boolean stopThread = false;

public void start() {
   // gives name to threads
    BasicThreadFactory factory = new BasicThreadFactory.Builder()
             .namingPattern("thread-%d").build();
    executorService =  Executors.newSingleThreadExecutor(factory);
    executorService.execute(new Runnable() {
        @Override
        public void run() {
            try {
                doTask();
            } catch (Exception e) {
                logger.error("indexing Failed",e);
            }

        }
    });
    executorService.shutdown();
}

private void doTask()  {
    logger.info("start reindexing of my objects");
    List<MyObjects> listOfMyObjects = new MyClass().getMyObjects();
    for (MyObjects myObject : listOfMyObjects) {
        if(stopThread){ // this is important to stop further indexing
            return;
        }
        DbObject dbObjects = getDataFromDB();
        // do some task
    }
}



public void stop() {
    this.stopThread = true;
    if(executorService != null){
        try {
            // wait 1 second for closing all threads
            executorService.awaitTermination(1,TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>