What is the use and need to localize the stream

I studied local thread in Java. I could not understand why we need this class. I can achieve the same motive if I simply just pass a new object for each thread to execute, as this happens if I use initialValue (). I just return a new object for each thread in initialvalue ().

But say that I have two threads: ThreadOne: A and ThreadTwo B. Now I want them to have a copy of their own SimpleDateFormat class. I can do this by deforming the SimpleDateFormat object in the ThreadLocal class, and then using initialValue (), I can return a new SimpleDateFormat ("yyyyMMdd HHmm") ;. The same motive that I can achieve by creating two new objects SimpleDateFormat and p [each of which is dedicated to ThreadOne: A. and ThreadTwo: B. How ThreadLocal helps me additionally

Hi,

+7
source share
4 answers

Below are some good examples here for your question.

But I'm trying to explain the second part:

But I will say that I have two threads: ThreadOne: A and ThreadTwo B. Now I want them to have a copy of their own SimpleDateFormat class. I can do this by deforming the SimpleDateFormat object in the ThreadLocal class and then using initialValue (), I can return a new SimpleDateFormat ("yyyyMMdd HHmm") ;. The same motive that I can achieve by creating two new objects SimpleDateFormat and p [each ThreadOne: A. and ThreadTwo: B. How ThreadLocal helps me additionally

Often you need to format dates with a specific format, and, of course, it is recommended that you create a SimpleDateFormat object once (instead of creating a new SimpleDateFormat for each time you need to format the date).

So you might have something like this:

 public class DateUtils { private final static DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy"); public String formatDate(Date date) { return dateFormat.format(date); } } 

This will happen if multiple threads call formatDate(...) at the same time (you may get weird output or exceptions ) because SimpleDateFormat not Thread-Safe . To make it thread safe, you can use ThreadLocal :

 public class DateUtils { private final ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() { @Override protected DateFormat initialValue() { return new SimpleDateFormat("dd-mm-yyyy"); } }; public String formatDate(Date date) { return dateFormat.get().format(date); } } 

Now each stream (or call) to formatDate () will work with a local copy and will not interfere with each other. This gives you thread safe behavior.

+4
source

Stream local storage serves for global variables in the context of a single stream.

Consider this example: you are writing a multi-threaded program to process user requests. Multiple users can initiate requests at the same time; your system uses one thread per user.

When a user request arrives, your system calculates the user from which it came, and creates an instance of the UserPermissions object for that user.

There are several ways to make this object available to your running program. One way is to pass UserPermissions each method that it may need, as well as to each method that directly or indirectly calls a method that it may need. This can be problematic, especially in a context where callbacks are used.

If your program was not multithreaded, you set UserPermissions to a global variable. Unfortunately, you cannot do this, because several user queries can be active at the same time.

This place includes a threaded local storage: a process that creates user permissions, installs a UserPermissions object in the local thread store and leaves it there until the request is processed. Thus, all methods can capture UserPermissions as needed, without having to pass them as method parameters.

+1
source

You will use ThreadLocal to transfer "data" to a specific thread.
For example, you have a doSomething(SomeObject a, SomeOtherObject b); method doSomething(SomeObject a, SomeOtherObject b);
You can pass additional information to this method for a specific thread of execution in thread safe mode through stream locators than just a and b

0
source

Local stream is a cheap way to implement dynamic scaling. With dynamic scaling, a binding exists during code block evaluation. In this case, the binding exists at runtime of a particular thread. Dynamic scaling was supported in early methods, but this rarely makes sense, so most modern programming languages ​​do not support it, except through stream locators.

Dynamic viewports / stream locators are useful for maintaining ubiquitous contextual information, such as:

There is a series of studies called " context-oriented programming ", the purpose of which is to improve support for such problems in programming languages. The document, Context-Oriented Programming: Beyond Layers , provides some additional examples.

0
source

All Articles