You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by "M. Niedermair" <m_...@gmx.de> on 2015/10/08 11:59:14 UTC

PDFont - thread save?

Hello there,

I load the font "Linux Libertine" in a factory as a singleton (I create 
several hundred PDFs with this font.).

libertine_R = PDTrueTypeFont.loadTTF(document,
    FontLibertine.class.getResourceAsStream("ttf/LinLibertine_Rah.ttf"));

In single-threaded, this also works without problems.
But if I start multiple threads that use the Font singelton, there is an 
error. I run four threads per core (4x8 = 32).

As a stopgap, I load the font in each thread individually.
Is there a better solution?

Michael

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: PDFont - thread save?

Posted by John Hewson <jo...@jahewson.com>.
No, it’s not thread safe. Nor is it safe to share a PDTrueTypeFont across multiple
PDDocument instances.

Read-only access to PDFs is “mostly” thread safe now, though we promise nothing,
however write-access to PDFs is definitely not.

— John

> On 8 Oct 2015, at 02:59, M. Niedermair <m_...@gmx.de> wrote:
> 
> Hello there,
> 
> I load the font "Linux Libertine" in a factory as a singleton (I create several hundred PDFs with this font.).
> 
> libertine_R = PDTrueTypeFont.loadTTF(document,
>   FontLibertine.class.getResourceAsStream("ttf/LinLibertine_Rah.ttf"));
> 
> In single-threaded, this also works without problems.
> But if I start multiple threads that use the Font singelton, there is an error. I run four threads per core (4x8 = 32).
> 
> As a stopgap, I load the font in each thread individually.
> Is there a better solution?
> 
> Michael
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 


Re: PDFont - thread save?

Posted by "M. Niedermair" <m_...@gmx.de>.
Hi,

 > However, it is my personal experience that you can easily parallelize
> certain tasks calling pdfbox library methods using a miminal amount of care
> and design in the way you invoke and synchronize the threads.
>

I have now changed my code that the font is stored in a 
ByteArrayInputStream (So he does not need to be loaded every time.).
Then a separate PDFont is created for each task.

    private static ByteArrayFileInputStream libertine_R = null;

    public static PDFont getLibertine_R(final PDDocument document) 
throws IOException {
       if (libertine_R == null) {
          synchronized (FontLibertine.class) {
             if (libertine_R == null) {
                libertine_R = new ByteArrayFileInputStream(
 
FontLibertine.class.getResourceAsStream("ttf/LinLibertine_Rah.ttf"));
             }
          }
       }
       PDFont font;
       synchronized (libertine_R) {
          libertine_R.reset();
          font = PDTrueTypeFont.loadTTF(document, libertine_R);
       }
       return font;
    }

The creation of PDFs is now 40 percent faster.

>     - Which framework do you use for your multi-threaded approach?

ThreadPoolExecutor and LinkedBlockingQueue<Runnable>

With the above solution I can work and create hundreds of PDFs in multi 
threads.

Thank you!
By
Michael


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: PDFont - thread save?

Posted by Roberto Nibali <rn...@gmail.com>.
Hi

Short answer: from the pdfbox FAQ entry (
https://pdfbox.apache.org/1.8/faq.html#threadsafe) point of view: no,
generally you cannot assume thread-safety with the pdfbox library.

However, it is my personal experience that you can easily parallelize
certain tasks calling pdfbox library methods using a miminal amount of care
and design in the way you invoke and synchronize the threads.

With regard to your issue, I'm afraid that you would need to provide more
information:

   - What is the exact error message you get?
   - How exactly is your libertine_R variable defined? E.g.: a global
   static final PDFont?
   - How do you use your libertine_R variable inside the threads?
   - Could you share some more of your code, especially the part of the
   worker threads?
   - Which framework do you use for your multi-threaded approach?


Best regards
Roberto




On Thu, Oct 8, 2015 at 11:59 AM, M. Niedermair <m_...@gmx.de> wrote:

> Hello there,
>
> I load the font "Linux Libertine" in a factory as a singleton (I create
> several hundred PDFs with this font.).
>
> libertine_R = PDTrueTypeFont.loadTTF(document,
>    FontLibertine.class.getResourceAsStream("ttf/LinLibertine_Rah.ttf"));
>
> In single-threaded, this also works without problems.
> But if I start multiple threads that use the Font singelton, there is an
> error. I run four threads per core (4x8 = 32).
>
> As a stopgap, I load the font in each thread individually.
> Is there a better solution?
>
> Michael
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>