Multithreading - FreeMarker Manual

- Home
- Manual
- Java API

- Contribute
- Report a Bug
- Download

Manual
Previous Next

# Multithreading

In a multithreaded environment `Configuration` instances, `Template` instances and data-models should be handled as immutable (read-only) objects. That is, you create and initialize them (for example with `set...` methods), and then you don't modify them later (e.g. you don't call `set...`). This allows us to avoid expensive synchronized blocks in a multithreaded environment. Beware with `Template` instances; when you get a `Template` instance with `Configuration.getTemplate`, you may get an instance from the template cache that is already used by other threads, so do not call its `set...` methods (calling `process` is of course fine).

The above restrictions do not apply if you access all objects from the same single thread only.

It is impossible to modify the data-model object or a shared variable with FTL, unless you put methods (or other objects) into the data-model that do that. We discourage you from writing methods that modify the data-model object or the shared variables. Try to use variables that are stored in the environment object instead (this object is created for a single `Template.process` call to store the runtime state of processing), so you don't modify data that are possibly used by multiple threads. For more information read: Variables, scopes

Previous Next