gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:20:01 26 4
gpt4 key购买 nike

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

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

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

我尝试以这种方式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 类应该有一个停止主动搜索的方法,比如 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com