Java 移动到Runnable

Java 移动到Runnable,java,multithreading,Java,Multithreading,我有一些遗留代码,这是从类中使用main方法调用的,以强制应用程序关闭并停止所有可能正在运行的删除线程 private void stopFileDeletionThreads() { //as activeCount could be wrong, repeat it until no DeletionThread is found boolean bFound = false; while (true) { bFound = false;

我有一些遗留代码,这是从类中使用main方法调用的,以强制应用程序关闭并停止所有可能正在运行的删除线程

private void stopFileDeletionThreads() {
    //as activeCount could be wrong, repeat it until no DeletionThread is found
    boolean bFound = false;
    while (true) {
        bFound = false;
        ThreadGroup tg = Thread.currentThread().getThreadGroup();
        if (tg != null) {
            int count = tg.activeCount();
            Thread[] tArray = new Thread[count];
            tg.enumerate(tArray);
            for (int i = 0; i < count; i++) {
                if (tArray[i].getClass().equals(DeletionThread.class)) {
                    bFound = true;
                    ((DeletionThread) tArray[i]).setRunStatus(false);
                }
            }
        }
        if (!bFound) {
            break;
        }
    }
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        _logger.finest("Ignored interrupted exception when waiting after thread directory removal.");
    }
}
启动删除线程的代码位于另一个类中:

DeletionThread dt = new DeletionThread(f);
dt.start();
我打算使用ExecutorService和Runnable类,而不是扩展线程。如果上述情况类似于:

ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(new DirectoryRemover(directory));
executorService.shutdown();
我的疑问是,当应用程序被强制关闭以停止这些删除线程时,如何复制stopFileDelectionThreads中的代码。也许这是不必要的,或者我可以让executor服务与守护进程线程一起工作,但我了解到这在处理I/o时不是一个好主意

另一个想法是,不是每次都在方法中创建一个newSingleThreadExecutor,而是创建一个newCachedThreadPool实例,可以使用getmethod访问该实例并在此基础上调用shutdown。但我不确定

记住,我不能重写所有的东西,小步骤。谢谢

我打算使用ExecutorService和Runnable类,而不是扩展线程

实际上,只要代码不做傻事,将扩展线程转换为可运行的实现几乎是微不足道的

class MyThread extends Thread {

    /**
     * Thread run method.
     */
    public void run() {
        System.out.println("Do something as a Thread.");
    }

}

class MyRunnable implements Runnable {

    /**
     * Thread run method.
     */
    public void run() {
        System.out.println("Do something as a Runnable.");
    }

}

public void test() {
    // Start the `Thread`
    Thread t = new MyThread();
    t.start();
    // Start the `Runnable`.
    Thread r = new Thread(new MyRunnable());
    r.start();
}
我提到的愚蠢的事情包括调用sleep而不是Thread.sleep等,并且通常可以简单地修复


如何将现有代码转换为使用执行器这一不那么琐碎的问题实际上需要更多的细节,比如什么触发了DeletionThread的创建。

旁注:现有DeletionThread不是线程安全的……您是否考虑过使用运行时类提供的shudownHook关闭执行器?
class MyThread extends Thread {

    /**
     * Thread run method.
     */
    public void run() {
        System.out.println("Do something as a Thread.");
    }

}

class MyRunnable implements Runnable {

    /**
     * Thread run method.
     */
    public void run() {
        System.out.println("Do something as a Runnable.");
    }

}

public void test() {
    // Start the `Thread`
    Thread t = new MyThread();
    t.start();
    // Start the `Runnable`.
    Thread r = new Thread(new MyRunnable());
    r.start();
}