java:threads完全没有完成run()方法?

时间:2014-09-29 11:32:45

标签: java multithreading executorservice threadpoolexecutor

我有一个程序,它从一个给定的根开始探索文件系统。程序递归地从根点探索所有目录并对tiff文件执行一个过程。确实它合并了每两个tiff文件。我使用ExecutorService和ThreadPoolExecutor来处理合并函数。

     ExecutorService exec = Executors.newFixedThreadPool(threadsNumber);
     inputDirectoryProcess( inputDir );
     exec.shutdown();
     try {
         exec.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
     } catch (InterruptedException e) {
         e.printStackTrace();  
     }

对于每个合并功能,我使用以下内容:

    exec.submit(new MultipageTask( a, Integer.toString( intDetector(directoryFiles.get(i + 1).getName())/2 ), out) );

以下是MultipageTask类:

        public void run() {

        Thread thread = Thread.currentThread();
        System.out.println("start by " + thread.getName() + " (" + thread.getId() + ")");

        Integer[] resoulutions = null;

        try {

            BufferedImage image[] = new BufferedImage[tifImages.length];
            for (int i = 0; i < tifImages.length; i++) {
                resoulutions = getsize(tifImages[i].getPath());
                SeekableStream ss = null;
                ss = new FileSeekableStream(tifImages[i]);
                ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", ss, null);

                PlanarImage op = null;
                op = new NullOpImage(decoder.decodeAsRenderedImage(0),
                        null, OpImage.OP_IO_BOUND, null);
                image[i] = op.getAsBufferedImage();
            }

            TIFFField[] fields = new TIFFField[3];
            // ResolutionUnit
            TIFFField fieldResUnit = new TIFFField(296, TIFFField.TIFF_SHORT, 1,
                    (Object) new char[] { 2 });
            fields[0] = fieldResUnit;

            // XResolution
            TIFFField fieldXRes = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1,
                    (Object) new long[][] { { resoulutions[0], 1 } });
            fields[1] = fieldXRes;

            // YResolution
            TIFFField fieldYRes = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1,
                    (Object) new long[][] { { resoulutions[1], 1 } });
            fields[2] = fieldYRes;

            TIFFEncodeParam params = new TIFFEncodeParam();
            params.setCompression( TIFFEncodeParam.COMPRESSION_GROUP4 );
            params.setExtraFields( fields );
            OutputStream out = new FileOutputStream(outDir + "\\" + image_name + ".tif");
            ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out, params);
            Vector vector = new Vector();
            for (int i = 1; i < tifImages.length; i++) {
                vector.add(image[i]);
            }
            params.setExtraImages(vector.iterator());
            encoder.encode(image[0]);
            out.close();
            System.out.println("end by " + thread.getName() + " (" + thread.getId() + ")");

        } catch (IOException e) {
            e.printStackTrace();  
        }
        increasetCount();
    }
}

public synchronized void increasetCount() {
    convertCount = convertCount + 2;
    updateBar( convertCount );
}

程序终止后,我注意到有些文件没有处理完毕。所以我记录了进入和退出线程,如上所示。在控制台中,我看到以下几行:

start by pool-2-thread-4 (30)
end by pool-2-thread-2 (28)
start by pool-2-thread-2 (28)
start by pool-2-thread-4 (30)
start by pool-2-thread-2 (28)
start by pool-2-thread-1 (27)
start by pool-2-thread-1 (27)
start by pool-2-thread-4 (30)
end by pool-2-thread-1 (27)
end by pool-2-thread-2 (28)
start by pool-2-thread-1 (27)
start by pool-2-thread-2 (28)

正如你所看到的那些线程一样,28开始,但没有完成。这导致失去一些合并过程和程序无法处理所有tiff文件。

注意:我应该提一下,当我增加threadsNumber时,缺少的增加!!

我该如何解决?我想确保每个线程都完成运行过程。

感谢并抱歉英语不好。

1 个答案:

答案 0 :(得分:2)

如果你没有共享资源而且它不是死锁/活锁,请尝试用try / catch包围你的代码

public class MultipageTask implements Runnable {
    private File[] tifImages;
    private String image_name;
    private String outDir;

    public MultipageTask(File[] tifImages, String image_name, String outDir) {
        this.tifImages = tifImages;
        this.image_name = image_name;
        this.outDir = outDir;
    }

    @Override
    public void run() {

        Thread thread = Thread.currentThread();
        System.out.println("start by " + thread.getName() + " (" + thread.getId() + ")");

            try{
                 // doing merge process
            }catch(Exception e){
              e.printStackTrace();
            }
            System.out.println("end by " + thread.getName() + " (" + thread.getId() + ")");


    }
}