如果我使用单线程并且一次只做一个图像,我有一个可以压缩多页 tiff 图像的 java 应用程序。但是,一旦我使用线程池使其成为多线程,事情就开始出错了。例如,如果我有一个包含 2 个 TIFF 图像的目录并使用 2 个线程运行我的应用程序,则第一个图像会被很好地压缩并且所有页面都在那里。但是第二个总是丢失除第一个之外的每一页。
如果我从目录中删除其中一个图像(无论是哪一个),然后使用单个线程再次运行它,它会为这两个图像压缩图像,并且每个图像都保持其页数。
我的压缩方法是在一个名为 TiffHandler 的类中,具体方法如下:
public synchronized void compress() throws IOException
{
System.out.println("NUMpAGES: " + numPages);
if(this.getSrcImageFile().length()<SIZE_THRESHOLD){
this.closeAllStreams();
return;
}
this.initOutStreams();
this.setImageEncoder(ImageCodec.createImageEncoder("tiff", this.getOutputStream(), null));
int compressionAlgorithm;
if(bitDepth == 1 || bitDepth == 8)
{
/*Cant use JPEG_TTN2 with images that have less than 8-bit samples/only have a bit-depth of 1.*/
compressionAlgorithm = TIFFEncodeParam.COMPRESSION_DEFLATE;
encodeParams.setCompression(compressionAlgorithm); //redundant with line above
}
else
{
System.out.println("Attempting to compress using JPEG_TTN2 with bit depth: " + bitDepth);
compressionAlgorithm = TIFFEncodeParam.COMPRESSION_JPEG_TTN2;
encodeParams.setCompression(compressionAlgorithm); //redundant with line above
}
this.setImageEncoder(ImageCodec.createImageEncoder("tiff", this.getOutputStream(), encodeParams));
Vector vector = new Vector();
if(numPages == 1){
for(int i = 0; i < numPages - 1; i ++) //i < bufferedImages.length OLD
{
System.out.println(i);
vector.add((bufferedImages[i]));
}
}else{
System.out.println("Using second case");
for(int i = 0; i < numPages; i ++)
{
System.out.println("Adding to vector image for file " + this.getSrcImagePath() + " " + i);
vector.add((bufferedImages[i]));
}
}
System.out.println("Buffered Images size: " + bufferedImages.length);
Iterator vecIter = vector.iterator();
if(numPages > 1){
vecIter.next();
}
encodeParams.setExtraImages(vecIter);
this.getImageEncoder().encode(bufferedImages[0]);
closeAllStreams();
}
而创建和运行线程的具体方法如下:
public void compressImages() throws IOException{
ExecutorService executor = Executors.newFixedThreadPool(NTHREADS);
for(int i = 0; i < TIFFList.size(); i ++)
{
TiffHandler newTiffHandler = new TiffHandler(TIFFList.get(i).getPath(), TIFFList.get(i).getPath());
Runnable worker = new ImgCompressor(newTiffHandler);
executor.execute(worker);
currCount++;
if(currCount%2 == 0)
{
updateProgress(currCount);
System.out.println((double)currCount/(double)imageCount);
}else if(i == TIFFList.size() - 1){
updateProgress(currCount);
}
}
executor.shutdown();
try
{
executor.awaitTermination(60, TimeUnit.SECONDS);
}
catch (InterruptedException ex)
{
Logger.getLogger(FJImageCompressor.class.getName()).log(Level.SEVERE, null, ex);
}finally{
this.mainProgBar.setString("Complete");
this.mainProgBar.update(this.mainProgBar.getGraphics());
}
}
另外,如果重要的话,这是我的可运行类:
import java.io.*;
public class ImgCompressor implements Runnable {
private ImageHandler mainHandler;
public ImgCompressor(ImageHandler mainHandler){
this.mainHandler = mainHandler;
}
@Override
public void run() {
try{
mainHandler.compress();
}catch(IOException e){
e.printStackTrace();
}
}
}
当 TiffHandler 类及其超级 ImageHandler 类中的几乎所有方法都同步时,我终生无法弄清楚为什么这些线程会相互混淆。
知道是什么原因造成的吗?