[英]How to stop threads of following type in Java used for watching folders for files using WatchService for folders by using jToggleButton
我想通过使用jToggleButton停止以以下方式生成的线程。 线程用于监视文件夹中的文件。 我尝试了很多,并进行了大量搜索,但未成功。 任何机构都可以提供帮助并建议任何解决方案来停止生成的线程。 即使按下jToggleButton,这些线程在Netbeans调试中仍显示为活动状态。 我尝试了用于停止的易失性条件,仅供参考:我有一个jToggle按钮,用于启动和停止线程。
该代码是由Netbeans生成的,因此有一些额外的代码,但是您可能只关注jToggleActionListener内部的代码和另一个类中的代码:谢谢您的帮助。
package threadnames;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jToggleButton1 = new javax.swing.JToggleButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jToggleButton1.setText("Stop");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(84, 84, 84)
.addComponent(jToggleButton1)
.addContainerGap(142, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jToggleButton1)
.addContainerGap(28, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
ExecutorService exec = Executors.newCachedThreadPool();
if (this.jToggleButton1.isSelected()) {
try {
// TODO add your handling code here:
Path home = Paths.get(System.getProperty("user.dir"));
WatchService watcher;
watcher = home.getFileSystem().newWatchService();
home.register(watcher, StandardWatchEventKinds.ENTRY_CREATE);
Runnable task = new FileWatch(watcher);
exec.submit(task);
boolean terminated;
terminated = exec.awaitTermination(1, TimeUnit.SECONDS);
if (terminated) {
System.out.println("All tasks completed.");
} else {
System.out.println("Some tasks are still running.");
}
} catch (IOException | InterruptedException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
exec.shutdownNow();
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JToggleButton jToggleButton1;
// End of variables declaration
}
这是run()的另一个类:
package threadnames;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.Watchable;
final class FileWatch implements Runnable {
private final WatchService watcher;
FileWatch(WatchService watcher) {
this.watcher = watcher;
}
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
break;
}
Watchable dir = key.watchable();
System.out.println(dir);
for (WatchEvent<?> evt : key.pollEvents()) {
System.out.println(" " + evt.context());
}
}
}
}
使用线程的中断状态终止循环。 这比您创建自己的标志更好,因为它使您的任务可与ExecutorService
一起使用; 您可以通过提交时收到的Future
取消特定任务,也可以使用shutdownNow()
中断所有任务。
除非您的任务在创建和管理的线程中运行,否则在检测到中断后重新声明中断状态是最安全的,以便调用者也可以处理它。 换句话说,所有线程和任务都需要具有定义的中断策略并相应地使用。
这是一个使用WatchService
的示例Runnable
任务:
final class FileWatch implements Runnable {
private final WatchService watcher;
FileWatch(WatchService watcher) { this.watcher = watcher; }
@Override
public void run()
{
while (!Thread.currentThread().isInterrupted()) {
WatchKey key;
try {
key = watcher.take();
}
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
break;
}
Watchable dir = key.watchable();
System.out.println(dir);
for (WatchEvent<?> evt : key.pollEvents()) {
System.out.println(" " + evt.context());
}
}
}
}
使用这种服务的方法如下:
public static void main(String... argv)
throws Exception
{
Path home = Paths.get(System.getProperty("user.home"));
WatchService watcher = home.getFileSystem().newWatchService();
home.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.OVERFLOW);
Runnable task = new FileWatch(watcher);
ExecutorService exec = Executors.newCachedThreadPool();
exec.submit(task);
Thread.sleep(3000);
exec.shutdownNow();
boolean terminated = exec.awaitTermination(1, TimeUnit.SECONDS);
if (terminated)
System.out.println("All tasks completed.");
else
System.out.println("Some tasks are still running.");
}
因为FileWatch
任务正确支持中断,所以您将看到此测试显示在调用shutdownNow()
之后所有任务都已完成。 如果将使用其他终止方法的任务添加到ExecutorService
,您将看到它们继续运行。
目前的代码存在两个问题。 这是对jToggleButton1ActionPerformed()
事件处理程序的分析,该事件处理程序在按下按钮时由Swing事件调度线程( EDT )调用。
When the button is pressed, create a new ExecutorService as a local variable. If toggle selected, submit a file watching task to the executor, and block the EDT for 1 second, or until the executor is shutdown. Otherwise, shutdown the newly-created executor. Discard reference to the executor.
第一个问题是,由于执行程序服务永远不会存储在局部变量之外的任何地方,一旦该方法退出,对该特定实例的引用将永远丢失,并且无法在其上调用shutdownNow()
。
第二个问题是,如果确实要阻止EDT(可能不是一个好主意)直到执行程序终止,则应在调用shutdownNow()
(在未选择toggle的情况下,在“ else”子句中)(而不是在提交之后)之后执行此操作任务。 再次查看上面的示例,您将看到我仅在关闭执行程序后才等待终止。
将ExecutorService变量从方法中吊起,并使其成为类的成员变量。 这将允许切换按钮处理程序访问ExecutorService
的相同实例并将其关闭。 然后,将等待终止移动到未选择的切换分支。
这是应该的流程:
When the button is pressed, If toggle selected, create a new executor service and assign it to a member variable, and submit a file watching task to the service. Otherwise, shutdown the executor, and wait for the service to terminate.
另外,您在这里使用newSingleThreadedExecutor()
就足够了。
一种方法是使用将volatile boolean
为true
的stop
方法。
public class HelloRunnable implements Runnable {
private volatile boolean stop = false;
public void run() {
if (!stop) {
System.out.println("Hello from a thread!");
}
}
public void stop() {
stop = true;
}
public static void main(String args[]) {
for (int i = 0; i < 5; i++) {
HelloRunnable hr = new HelloRunnable();
new Thread(hr).start();
hr.stop();
}
}
}
如果线程可能被阻塞,您可以安排中断它,但是当然不能保证中断线程,因为它可能不会被阻塞,只是忙。
public class HelloRunnable implements Runnable {
private volatile boolean stop = false;
private volatile Thread thread = null;
public void run() {
thread = Thread.currentThread();
if (!stop) {
System.out.println("Hello from a thread!");
}
}
public void stop() {
stop = true;
if ( thread != null ) {
thread.interrupt();
}
}
public static void main(String args[]) {
for (int i = 0; i < 5; i++) {
HelloRunnable hr = new HelloRunnable();
new Thread(hr).start();
hr.stop();
}
}
}
如果使用WatchService.poll(...)或WatchService.take(),则这最后一种技术也应该起作用。
如果忙于大多数IO进程,它也应该中断线程。
有一个Thread.stop()
方法,但已被弃用 ,因为它是不安全的。
您可以修改一些变量以指示目标线程应停止运行,而不是使用不建议使用的方法。
您可以在run
方法中使用一些flag
来检查是否退出该方法,这样就可以间接退出run
方法。 目前不建议通过任何其他方法停止线程。 见链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.