如何在Java中停止线程? [英] How to stop threads in Java?

查看:85
本文介绍了如何在Java中停止线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用GUI创建了一个java程序,我想要一个停止按钮功能,当用户点击停止按钮时,程序必须停止。

I have made a java program with GUI and I want a stop button functionality in which when a user clicks on the stop button, the program must be stopped.


  1. 在我的程序中,主线程启动其他10个线程,我希望每当单击停止按钮时,必须在主线程之前停止所有10个线程。

  1. In my program, the main thread starts other 10 threads and I want that whenever the stop button has been clicked all the 10 threads must be stopped before the main thread.

其次,我还希望每当这10个线程的任何线程停止时,它必须首先关闭它之前打开的所有资源,比如连接到数据库等。

Second, I also want that whenever any thread of those 10 threads is stopped, it must first close all the resources it had opened before like connection to a database etc.

我已经实现了代码........

I have implemented the code as answered by ........

现在有一个问题。

我的线程类是这样的:

public class ParserThread implements Runnable {
    private volatile boolean stopped = false;

    public void stopTheThread() {
        stopped = true;
    }
    :
    :
}

下面是从函数start()开始10个线程的主线程

And below is the main thread that starts 10 threads from the function start()

public class Main() {
    Thread [] threads;

    public void start() {
        for(int i = 0; i < 10; i++) {
            threads[i] = new Thread(new ParserThread());
        }       
    }

    public void stop() {
        // code to stop all the threads
    }
}

现在我想调用ParserThread的stop方法来设置stopped = true来停止线程。我想要为所有10个线程完成这个事情。

Now I want to call the stop method of the ParserThread to set "stopped = true" to stop the thread. I want this thing to be done for all the 10 threads.

如何调用该stop方法。我希望它可以在Main类的stopAllThreads()方法中完成。

How can I call that stop method. I want it to be done in the stopAllThreads() method of the Main class.

推荐答案

一般来说,这样做的方法是让每个其他线程定期检查一个标志。通常后台线程循环,等待工作 - 他们只需要在每次循环时检查标志。如果他们使用 Object.wait()或类似的东西被告知有更多工作,则应该使用相同的通知来指示线程也应该停止。 (不要只是旋转,直到你停止 - 这将吸收CPU。不要只是使用睡眠 - 这将延迟终止。)

Generally speaking, the way to do this is to have each of the other threads periodically check a flag. Often background threads loop, waiting for work - they just have to check the flag each time they go round a loop. If they're using Object.wait() or something similar to be told that there's more work, the same notification should be used to indicate that the thread should stop too. (Don't just spin until you're stopped - that will suck CPU. Don't just use sleep - that will delay termination.)

这允许所有线程干净地终止,适当地释放资源。 IMO,其他选项,如 interrupt()和不推荐的 destroy()方法更难控制。 (中断线程比硬中断更好,但它有一系列问题 - 例如中断只在某些点处理。)

That allows all threads to terminate cleanly, releasing resources appropriately. Other options such as interrupt() and the deprecated destroy() method are much harder to control properly, IMO. (Interrupting a thread is better than hard-aborting it, but it has its own set of problems - such as the interruption is only processed at certain points anyway.)

编辑:在代码中,它看起来像:

In code, it would look something like:

// Client code
for (Task task : tasks) {
  task.stop();
}

// Threading code
public abstract class Task implements Runnable {

  private volatile boolean stopped = false;

  public void stop() {
    stopped = true;
  }

  protected boolean shouldStop() {
    return stopped;
  }

  public abstract void run();
}

您的任务将继承任务。如果您希望 stop()方法也通知监视器,您需要使其稍微复杂一点,但这是基本的想法。

Your tasks would then subclass Task. You would need to make it slightly more complicated if you wanted the stop() method to also notify a monitor, but that's the basic idea.

示例任务:

public class SomeTask extends Task {
  public void run() {
    while (!shouldStop()) {
      // Do work
    }
  }
}

这篇关于如何在Java中停止线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆