java - 执行某些操作时停止应用程序

标签 java search task

我创建了一个应用程序,用于搜索目录中包含的每个文件中的单词。 我想实现一种停止搜索的方法。 我创建了一个方法,根据在目录中找到的每种文件类型调用其他不同的方法。 我曾在一个类(class)中工作,并创建了一个请求停止搜索的 JButton:

 private void jButtonActionPerformed(java.awt.event.ActionEvent evt) 
    {                                         
            System.out.println("Requesting stop");
    }   

我需要一种方法来停止搜索目录的方法,假设我的方法是 search(directory);

我尝试以这种方式搜索(目录);:

private Thread a;
a = new Thread(new Runnable() { 
     @Override
     public void run() {
       search(directory);
     }
        } 
  );

然后在搜索时单击jButton 停止搜索:

 private void jButtonActionPerformed(java.awt.event.ActionEvent evt) 
    {             
            System.out.println("Requesting stop");                            
            a.stop(); 
    } 

它有效,但我已经阅读了很多关于此的帖子,这不是停止线程的好方法。 那么还有其他选择吗?你能举个例子吗?

我也尝试过这个:

java.util.TimerTask timerTask;
ScheduledThreadPoolExecutor scheduledThread = new ScheduledThreadPoolExecutor(10);
timerTask = new  java.util.TimerTask() {
        @Override
        public void run() {
            search(directory);
        }
    };
scheduledThread.schedule(timerTask, 3, TimeUnit.MILLISECONDS);

然后在jButtonActionPerformed中:

 private void jButtonActionPerformed(java.awt.event.ActionEvent evt) 
    {                                         
       System.out.println("Requesting stop");
       scheduledThread.shutdownNow();
    } 

它也有效,但在这种情况下,当我尝试执行此操作时,我无法开始新的搜索。我需要关闭并重新打开应用程序。

最佳答案

一种方法:

  • 执行搜索的代码应该位于其自己的类中。
  • 您的 GUI Controller (例如 ActionListener)将创建此类的对象。
  • 当您的 GUI 希望 Search 类进行搜索时,它应该调用该类的方法,也许是 beginSearch() 并在后台线程上执行此操作。
  • Search 类应该有一个停止 Activity 搜索的方法,例如 GUI Controller 可以调用的 stopSearch()
  • 此方法可能会设置搜索对象的一个​​ boolean 字段,一个标志,例如称为 continueSearching 或类似的东西,其搜索方法在其搜索过程中间歇性检查该字段。搜索。如果 boolean 值设置为 false,则搜索应停止。

另请注意,您是正确的。在线程上调用 stop() 不仅不推荐,而且非常危险。请看看Thread API是什么?关于这个问题不得不说:

Deprecated. This method is inherently unsafe. Stopping a thread with Thread.stop causes it to unlock all of the monitors that it has locked (as a natural consequence of the unchecked ThreadDeath exception propagating up the stack). If any of the objects previously protected by these monitors were in an inconsistent state, the damaged objects become visible to other threads, potentially resulting in arbitrary behavior. Many uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. If the target thread waits for long periods (on a condition variable, for example), the interrupt method should be used to interrupt the wait.

另请查看此链接:Why is Thread.stop deprecated? .


编辑
您声明:

the problem is that my application code is long and it will be hard to edit it like your way,...

你的类(class)越长,尝试将其分解为单独的逻辑单元就变得越重要。否则,您将陷入调试和增强的噩梦。

i'm not an expert in programming and i'm still studying for that, that's why I wanted to know whether there was a way according to my situation.

你练习重构的次数越多,你就会做得越好,也就越容易。现在就是开始的好时机。

关于java - 执行某些操作时停止应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17641578/

相关文章:

java - firebase实时数据库验证规则

php - 谷歌是否抓取/索引 "computed"或原始html源?

android - 使用 searchview 和搜索建议以及语音搜索过滤 recyclerview

delphi - 计算给定路径的子目录数

c# - 如何避免循环多线程任务中 JSON 序列化期间集合修改?

c# - Kinect 框架异步到达

Java Eclipse : Hibernate Configuration to AnnotationConfiguration does not work and gives the runtime error

java - 直接自引用导致循环父类(super class)问题 JSON

java - 如何使用 java sdk 查找 Google DataFlow 中每个步骤所花费的总执行时间

c# - 具有可变输入参数数量的 MSBuild 自定义任务