如何将方法重建为线程

时间:2014-12-09 14:47:18

标签: java multithreading runnable

如何重建我的方法:

public class PDFCheck {
        public static void testAllFontsAreEmbedded(PDFDocument pdf) throws PDFDocumentException {

                for (PDFFont font : pdf.listFonts()) {

                    if (!font.isEmbedded()) {
                        errorMessageBuffer.append("font not embedded: " + font.getName() + "\n");
                        fontError = "font error";
                    }
               }

进入这样的线程:?

public class Task1 implements Runnable {
    public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                       ................
                       ................
                       ................
                       ................
           }
    }
}

主要是我会这样做:

Thread t1 = new Thread(new Task1());
t1.start();
t1.interrupt();

我想这样做,因为我已经开发了一个pdf检查工具,当pdf太大而且检查时间太长而无法检查时,停止按钮应该停止“font chek”(参见上面的代码片段)。 p>

我试过这个来构建一个构造函数,但构造函数显示了很多错误消息:

public void run() {
    while (!Thread.currentThread().isInterrupted()) {

    public static  void testAllFontsAreEmbedded(PDFDocument pdf) throws PDFDocumentException {

        for (PDFFont font : pdf.listFonts()) {

            if (!font.isEmbedded()) {
                fontError = "font error" + " | ";
            } else {
                fontError = "";
            }
        }
            System.out.println("läuft");
        }
    }
}

更新:我最后在这个方法中集成了一个Thread。现在的问题是该方法只是一直选择路径的第一个pdf文件...我的 while 语句是否处于错误的位置?

new Thread() {
                @Override
                public void run() {
                    String directory;
                    directory = "C:\\Users\\Tommy\\Desktop\\pdf";
                    File inputFiles = new File(directory);
                    CopyOfSettingsGui.this.running = true;
                    for (File file : inputFiles.listFiles()) {
                        if (file.isFile()) {

                            if (file.getName().endsWith((".pdf"))) {
                        while (CopyOfSettingsGui.this.running) {

                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                            System.out.print(file.getName() + "\n");

                        }
                        return;
                    }
                        }
                    }
                }
            }.start();

2 个答案:

答案 0 :(得分:1)

你可以按照Conffusion的答案中的描述传递pdf,但是如果你想使用interrupt(),你必须使你的线程“可以中断”除非你的线程正在调用方法,否则这个方法不会停止线程抛出'Interrupted Exception'或检查'中断标志'。所以你必须在每次迭代中调用'Thread.interrupted()'。

public void run() {
    for (PDFFont font : pdf.listFonts()) {
       if (Thread.interrupted()){
           return;
       }
       ...
    }
}

或者,您可以设置成员变量来停止线程:

class PDFCheckThread extends Thread {

    private boolean stop;

    public PDFCheckThread(PDFDocument pdf) {
        this.pdf = pdf;
    }

    public void setStopFlag() {
        stop = true;
    }

    public void run() {
        for (PDFFont font : pdf.listFonts()) {
            if(stop) {
                return;
            }
            ...
        }
    }
}

答案 1 :(得分:0)

您必须将PDF传递给Task1构造函数并将其存储在本地变量中(在Task1中。在run()方法中,您可以访问PDF文档:

public class Task1 implements Runnable {
    private PDFDocument pdf;
    public Task1 (PDFDocument pdf) {
        this.pdf=pdf;
    }
    public void run() {
        for (PDFFont font : pdf.listFonts()) {
            if (!font.isEmbedded()) {
                fontError = "font error" + " | ";
            } else {
                fontError = "";
            }
        }
            System.out.println("läuft");
        }
    }
}

启动主题:

Thread t1 = new Thread(new Task1(myPDFInstance));
t1.start();
t1.interrupt();
相关问题