title: Multi threaded approach to convert TIFF to PDF using iText
body: <p>I am essentially trying to convert a tiff file to pdf using itext, which is fairly simple.
  But from what I can see <code>TiffImage.getTiffImage</code> is taking a lot of time to execute for larger files.</p>
  
  <p>My requirement is to use <code>FutureTask</code> and <code>ExecutorService</code> to provide a multi threaded solution. Here is my current code:</p>
  
  <pre><code>import java.util.concurrent.Callable;
  
  import com.itextpdf.text.Image;
  import com.itextpdf.text.pdf.RandomAccessFileOrArray;
  import com.itextpdf.text.pdf.codec.TiffImage;
  
  public class ProcessTiffImage implements Callable&lt;Image&gt;{
  
      RandomAccessFileOrArray tiffFile;
      int pageNo;
  
      public ProcessTiffImage(RandomAccessFileOrArray tiffFile, int pageNo){
          this.tiffFile = tiffFile;
          this.pageNo = pageNo;
      }
  
      public Image call() throws Exception {
          Image image = TiffImage.getTiffImage(tiffFile, pageNo);
  
          return image;
  
      }
  
  }
  </code></pre>
  
  <p>and the convert method is</p>
  
  <pre><code>public boolean convert(Document document) {
  
          int numOfThreads = Runtime.getRuntime().availableProcessors()  ;
          ExecutorService service = Executors.newFixedThreadPool(numOfThreads );
          List&lt;FutureTask&lt;Image&gt;&gt; taskList = new ArrayList&lt;FutureTask&lt;Image&gt;&gt;();
          List&lt;Image&gt; imageList = new ArrayList&lt;Image&gt;();
  
          for (int page = 1; page &lt;= numOfPages; page++) {
              FutureTask&lt;Image&gt; futureTask = new FutureTask&lt;Image&gt;(new ProcessTiffImage(tiffFile, page));
              taskList.add(futureTask);
              service.execute(futureTask);
          }
  
          try {
              // Wait until all results are available
              for (FutureTask&lt;Image&gt; future : taskList) {
  
                  imageList.add(future.get());
              }
  
          } catch (InterruptedException ex) {
              ex.printStackTrace();
          } catch (ExecutionException ex) {
              ex.printStackTrace();
          }
  
          service.shutdown();
  
  
          boolean success = generatePdf(document, imageList);
          return success;
      }
  </code></pre>
  
  <p>But I'm getting a <code>NullPointerException</code> at <code>future.get()</code>. The problem is the execution is not waiting for TiffImage.getTiffImage(tiffFile, pageNo) to finish. Hence I'm unable to create the Image List.
  Any help would be very much appreciated.</p>
  
  <p>Stack Trace</p>
  
  <pre><code>java.util.concurrent.ExecutionException: java.lang.NullPointerException
      at java.util.concurrent.FutureTask.report(FutureTask.java:122)
      at java.util.concurrent.FutureTask.get(FutureTask.java:188)
      at com.app.convertor.TiffParser.convert(TiffParser.java:107)
      at com.app.start.TiffToPdf.main(TiffToPdf.java:40)
  Caused by: java.lang.NullPointerException
  Caused by: java.lang.NullPointerException
      at com.itextpdf.text.pdf.codec.TIFFDirectory.getFieldAsLong(TIFFDirectory.java:467)
      at com.itextpdf.text.pdf.codec.TIFFDirectory.getFieldAsLong(TIFFDirectory.java:477)
      at com.itextpdf.text.pdf.codec.TiffImage.getTiffImage(TiffImage.java:124)
      at com.itextpdf.text.pdf.codec.TiffImage.getTiffImage(TiffImage.java:106)
      at com.app.processor.ProcessTiffImage.call(ProcessTiffImage.java:20)
      at com.app.processor.ProcessTiffImage.call(ProcessTiffImage.java:1)
      at java.util.concurrent.FutureTask.run(FutureTask.java:262)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:744)
  </code></pre>