如何使用停止按钮停止运行Java代码 [英] How to stop a java code from running using a stop button

查看:155
本文介绍了如何使用停止按钮停止运行Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用来自支持Bean的方法的按钮。此方法允许从解析html代码中提取数据。当该方法运行时,我有一个对话框,显示进度条和命令按钮取消。我需要当用户点击取消按钮时,提取按钮调用的方法停止。

I have a button that calls a method from the backing Bean. This method allows to extract data from parsing html code. While the method is running i have a dialog showing a progress bar and a command button Cancel. I need when the user click the cancel button the method called by the extract button stops.

这是我的HTML代码:

This is my html code:

<p:commandButton
    value="Start" style="width: 12%;height: 100%"
    update=":confirmPurchase, :confirmPurchaseTest, :mainform" id="extractbutton"
    ajax="true" widgetVar="ButtonExtract"
    actionListener="#{mailMB.searchEmails()}" 
    icon="ui-icon-disk" styleClass="ui-priority-primary"
    onstart="blockUIWidget1.show();" oncomplete=" blockUIWidget1.hide();" />

<p:dialog  widgetVar="blockUIWidget1" header="Hitonclick" modal="true"
    resizable="false" closable="false">
    <table border="0" style="width: 500px">
        <tbody > 
            <tr>  
                <td>
                    <p:graphicImage url="pictures/loading81.gif" width="200" height="200" alt="animated-loading-bar"/>
                </td>
                <td>
                    <h:outputLabel value="Extracting is in progress. Please wait..."/>
                    <div align="center">
                        <p:commandButton value="Cancel" title="Cancel" />
                    </div>
                </td>
            </tr>
            <div align="right">

            </div>
        </tbody>
    </table>
</p:dialog>

这是我在sessionScoped Bean中的searchEmails方法

And here is my searchEmails method in my sessionScoped Bean

 public void searchEmails() throws Exception {
        idCustomer = (String) session.getAttribute("idCustomer");
        System.out.println(idCustomer + " this is it");
        Customer customer = customerBusinessLocal.findById(idCustomer);
        data = dataBusinessLocal.createData(new Date(), number, keyword, moteur, customer, State.REJECTED);
        mails = mailBusinessLocal.createEmails(keyword, number, moteur, data);
        System.out.println("Method was invoked");    
 }

如何通过取消命令按钮停止searchEmails方法的运行?

How can i stop the searchEmails method from running via the cancel command button?

推荐答案

具有中断友好任务的ExecutorService



ExecutorService 文档


  • 不是直接调用方法,而是转换方法进入 ExecutorService 的任务。

public class SearchEmailsTask implements Runnable {

    private EmailSearcher emailSearcher;

    public SearchEmailsTask(EmailSearcher emailSearcher) {
        this.emailSearcher = emailSearcher;
    }

    @Override
    public void run() {
        emailSearcher.searchEmails();
    }
}

您可以使用 Callable<对象> 如果你想要退货。

You can use Callable<Object> if you want to return something.


  • 如果要调用方法,请将该任务提交到 ExecutorService

ExecutorService executorService = Executors.newFixedThreadPool(4);

SearchEmailsTask searchEmailsTask = new SearchEmailsTask(new EmailSearcher());

Future<?> future = executorService.submit(searchEmailsTask);



  • 保留对任务的引用。

  • Keep a reference to the task.

private static Map <String, Future<Object>> results = new HashMap <String, Future<Object>>();

存储多个 Future 对象。如果你愿意,你当然可以选择更好的东西。

A map should be a good idea to store multiple Future objects. You can of course go for something better if you want.


  • 在需要时调用任务取消。

  • Call cancel on the task whenever required.

future.cancel(true);


注意:

任务应该对线程中断进行适当的检查以便正确取消。

要实现这一点,请参阅

Note:
Task should have suitable checks for thread interruption for proper cancellation.
To achieve this, refer to


  1. 未来的任务ExecutorService并未真正取消

  2. 如何使用线程的ID暂停线程?

  1. Future task of ExecutorService not truly cancelling
  2. how to suspend thread using thread's id?

祝你好运。

这篇关于如何使用停止按钮停止运行Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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