java线程池停止线程_如何在Java中停止线程?

java线程池停止线程

停止线程 (Stopping a thread)

  • As we know that there are no direct or shortcut ways to stop thread in Java.

    众所周知,在Java中没有直接或快捷的方式来停止线程。

  • As we know thread in java stops when the execution of run() method completed normally or thread stops if it raises an exception in the meanwhile of Thread completion.

    众所周知,当run()方法的执行正常完成时,java中的线程会停止;如果在线程完成的同时它引发异常,则线程会停止。

  • In previous versions of Java JDK there exists a method of Thread class named "stop" but this method is deprecated in later versions so it does not support in later versions of Java.

    在Java JDK的早期版本中,存在一个名为“ stop”的Thread类方法,但是该方法在更高版本中不推荐使用,因此在更高版本的Java中不支持。

  • Java does not provide some direct methods to stop thread in Java but there are some control methods defined in java JDK earlier versions and the names of the method are given below:

    Java没有提供一些直接的方法来停止Java中的线程,但是在Java JDK早期版本中定义了一些控制方法,该方法的名称如下:

    1. stop()
    2. suspend()
    3. resume()
  • All the methods given above are deprecated so it does not support in later versions of Java.

    上面给出的所有方法均已弃用,因此在更高版本的Java中不支持。

  • We will see how to stop a thread implicitly Java JDK stops a thread implicitly if and only if either a thread run() method completes it execution normally or if a thread raises an exception in the meanwhile.

    我们将看到如何隐式停止线程Java JDK仅在以下情况下隐式停止线程:(仅当线程run()方法正常完成了该线程的执行)或同时线程引发异常。

  • Now, we will see how to stop a thread manually: we can stop a thread manually in two ways:

    现在,我们将看到如何手动停止线程:我们可以通过两种方式手动停止线程:

    1. With the help of volatile boolean variable
    2. With the help of interrupt() method

Now we will elaborate on each way one by one is given below...

现在,我们将在下面逐一详细说明每种方式...

With the help of volatile boolean variable

借助volatile布尔变量

  • In the first step, we will declare a volatile boolean variable in a thread.

    第一步,我们将在线程中声明一个易失的布尔变量。

  • In the second step initially, we will assign the value of the volatile boolean variable as true.

    首先,在第二步中,我们将volatile布尔变量的值分配为true。

  • In the third step, we will define a job inside while loop and passing parameter of the volatile boolean variable in while loop and we will keep loop inside run() method. By using loop the thread will continue to run until volatile boolean variable becomes false.

    在第三步中,我们将在while循环中定义作业,并在while循环中传递易失布尔变量的参数,并将循环保留在run()方法中。 通过使用循环,线程将继续运行,直到volatile布尔变量变为false。

  • In the fourth step, we will define another method named "stopThread()" inside a thread and in this method, we will set volatile boolean variable is set to false to stop the thread.

    在第四步中,我们将在线程内定义另一个名为“ stopThread()”的方法,在此方法中,我们将volatile布尔变量设置为false以停止线程。

  • In the fifth or final step, we don’t need to do anything manually to stop the thread. To stop a thread we can simply call user-defined stopThread() method to stop the thread.

    在第五步或最后一步,我们不需要手动执行任何操作即可停止线程。 要停止线程,我们可以简单地调用用户定义的stopThread()方法来停止线程。

Example:

例:

// Java program to stop a thread with the help of 
// volatile boolean variable

class ChildThread extends Thread {
    // intially assign boolean variable with value as true
    volatile boolean thread_stop = true;

    // this method is defined to stop a thread
    public void stopThread() {
        thread_stop = false;
    }

    // this loop will continue until boolean variable become false
    public void run() {
        while (thread_stop) {
            System.out.println("Child Thread Started");
        }
        System.out.println("Child Thread Ended");
    }
}

class Main {
    public static void main(String[] args) {
        ChildThread ct = new ChildThread();
        ct.start();
        try {
            Thread.sleep(100);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        // this method will be called to stop a thread
        ct.stopThread();
    }
}

Output

输出量

Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
.
.
.
.
.
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Ended

With the help of interrupt() method

借助interrupt()方法

We will use readymade interrupt() method to stop a thread.

我们将使用现成的interrupt()方法来停止线程。

When we call interrupt method on a thread object and it will assign the interrupted status of a thread and this status can be generated by the interrupted() method.

当我们在线程对象上调用中断方法时,它将分配线程的中断状态,并且此状态可以由interrupted()方法生成。

Example:

例:

// Java program to stop a thread by using interrupt() method

class ChildThread extends Thread {
    // this loop will continue until boolean variable become false
    public void run() {
        while (!Thread.interrupted()) {
            System.out.println("Child Thread Started");
        }
        System.out.println("Child Thread Ended");
    }
}

class Main {
    public static void main(String[] args) {
        ChildThread ct = new ChildThread();
        ct.start();
        try {
            Thread.sleep(100);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        // this method will be called to interrupt a thread
        ct.interrupt();
    }
}

Output

输出量

Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
.
.
.
.
.
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Ended


翻译自: https://www.includehelp.com/java/how-to-stop-a-thread-in-java.aspx

java线程池停止线程

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值