thread confinement

https://www.quora.com/What-is-Thread-Confinement

Thread confinement is the practice of ensuring that data is only accessible from one thread. Such data is called thread-local as it is local, or specific, to a single thread.

For example, declaring local variables is a form of thread confinement because each thread has its own local stack. The local variables you declare, are to be used for say thread A only. If another thread B comes in, it cannot use thread A’s local stack variables. Each thread has its own stack, and manipulates its own stack. This is called Thread local data.

Thread-local data is thread-safe, as only one thread can get at the data, which eliminates the risk of races. And because races are nonexistent, thread-local data doesn’t need locking. Thus thread confinement is a practice that makes your code safer (by eliminating a huge source of programming error) and more scalable (by eliminating locking).

An example of this would be a Servlet’s GET or POST method. In such a GET/POST method, if you were to declare local variables, these variables are to be confined to the specific thread that comes in. No other thread are sharing those variables.

Let’s say you declare member variable for the Servlet class. That member variable has global scope, and thus, can be shared by threads. This will lead to lots of problems, such as race conditions, or locking issues, and thus, we do NOT have thread confinement.