如何停止应用程序服务器中的无限线程

发布于 2024-12-12 09:23:36 字数 1877 浏览 0 评论 0原文

我在 glassfish 下部署了一个 JSF Web 应用程序,其中有两个按钮。第一个按钮启动无限线程,第二个按钮停止它。我的问题是我无法停止正在运行的线程。我在网上搜索了解决方案,但是如果我有 J2SE 应用程序但没有 J2EE 应用程序,它会起作用 这是我的代码

package com.example.beans;

import org.apache.commons.lang.RandomStringUtils;

public class MyBusinessClass {
    public static void myBusinessMethod() {
        /* this method takes a lot of time */
        int i = 1;
        while (i == 1) {
            String random = RandomStringUtils.random(3);

            System.out.println(random);

        }
    }
}   


package com.example.beans;

import java.util.Random;
import java.util.TimerTask;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.log4j.Logger;

import com.example.core.RandomUtils;

public class MySimpleRunnableTask implements Runnable {
private Logger logger = Logger.getLogger(MySimpleRunnableTask.class);

    @Override
    public void run() {
        MyBusinessClass.myBusinessMethod();
    }
}


@ManagedBean(name = "MainView")
@SessionScoped
public class MainView {

    private static Thread myThread;

    @SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
    public String startSimpleThread() throws SecurityException,
                                             NoSuchMethodException,
                                             InterruptedException {

        MySimpleRunnableTask mySimpleRunnableTask = new MySimpleRunnableTask();
        myThread = new Thread(mySimpleRunnableTask);
        myThread.start();
        return null;
    }

    @SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
    public String stopSimpleThread() throws SecurityException,
                                            NoSuchMethodException, 
                                            InterruptedException {
        myThread.interrupt();
        return null;
    }
}

我已经更改了我的代码,以便您可以真正理解我的问题是什么

i have a JSF web application deployed under glassfish in which i have two buttons.The first start a infinite thread and the second stop it.My problem is that i can not stop a running thread.I have searched for a solution on the net but in vain.it works in case i have a J2SE application but not with a J2EE application here is my code

package com.example.beans;

import org.apache.commons.lang.RandomStringUtils;

public class MyBusinessClass {
    public static void myBusinessMethod() {
        /* this method takes a lot of time */
        int i = 1;
        while (i == 1) {
            String random = RandomStringUtils.random(3);

            System.out.println(random);

        }
    }
}   


package com.example.beans;

import java.util.Random;
import java.util.TimerTask;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.log4j.Logger;

import com.example.core.RandomUtils;

public class MySimpleRunnableTask implements Runnable {
private Logger logger = Logger.getLogger(MySimpleRunnableTask.class);

    @Override
    public void run() {
        MyBusinessClass.myBusinessMethod();
    }
}


@ManagedBean(name = "MainView")
@SessionScoped
public class MainView {

    private static Thread myThread;

    @SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
    public String startSimpleThread() throws SecurityException,
                                             NoSuchMethodException,
                                             InterruptedException {

        MySimpleRunnableTask mySimpleRunnableTask = new MySimpleRunnableTask();
        myThread = new Thread(mySimpleRunnableTask);
        myThread.start();
        return null;
    }

    @SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
    public String stopSimpleThread() throws SecurityException,
                                            NoSuchMethodException, 
                                            InterruptedException {
        myThread.interrupt();
        return null;
    }
}

I have changed my code so you can understand really what's my problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷路的信 2024-12-19 09:23:36

中断只是将线程中的中断状态设置为true。线程需要定期池化中断状态标志以停止运行:

public void run() {
    /* you will have to touch the code here */
    int i = 1;
    while (i == 1) {
        String random = RandomStringUtils.random(3);
        logger.info(random);
        if (Thread.currentThread().isInterrupted()) {
            // the thread has been interrupted. Stop running.
            return;
        }
    }
}

这是正确停止线程的唯一方法:要求他停止。如果没有正在运行的线程的配合,就没有干净的方法。

interrupt only sets the interrupt status in the thread to true. The thread needs to regularly pool the interrupt status flag to stop running:

public void run() {
    /* you will have to touch the code here */
    int i = 1;
    while (i == 1) {
        String random = RandomStringUtils.random(3);
        logger.info(random);
        if (Thread.currentThread().isInterrupted()) {
            // the thread has been interrupted. Stop running.
            return;
        }
    }
}

This is the only way to properly stop a thread : ask him to stop. Without cooperation from the running thread, there is no clean way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文