CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
ThreadStaticAttribute is used on a static variable to create a separate variable for each thread executing it, rather than sharing (default behavior) the static variable across threads. This means that a static variable with the ThreadStaticAttribute is not shared across different threads accessing it. Each thread accessing it will have a separate copy of the same variable. If one thread modifies the variable, another thread accessing it will not be able to see the changes. This behavior is contrary to the default behavior of static variables. In short, ThreadStaticAttribute gives us the best of both worlds (static and instance).
The following listing shows the use of ThreadStaticAttribute (WroxShared.cs):
using System;The output from WroxStatic is:
i = 1 ThreadID = 2 x(static attribute)= 1 y = 2We all know a static variable is a class variable and its value remains the same across multiple objects of the class. ThreadStaticAttribute allows each thread accessing a static variable to have its own copy. In WroxStatic, variable x has ThreadStaticAttribute applied to it. As a result, each of the threads t1 and t2 will have a separate copy of the static variable x and changes made to x by thread t1 will not be visible to thread t2. On the other hand, changes made to the variable y by thread t1 will be visible to thread t2. If you observe the output of the program, variable x is incremented separately for threads t1 and t2.
The difference between a static variable with a ThreadStaticAttribute and an instance variable is that the static variable does not require an object to access it, whereas an exception will be thrown if you try to access an instance variable without creating the instance of an object.
Synchronization carries the overhead of the time required to acquire the synchronization lock. As a result, the performance is always poorer than the non-thread-safe version. As multiple threads might be trying to access objects at the same time to acquire the synchronization lock, the performance of the entire application might be affected inadvertently. This is a tradeoff a developer must be aware of when designing larger applications. The important part is that these thread contentions are not visible until a thorough stress test is performed. Stress testing is extremely important in designing large-scale multithreaded applications. The developer has to balance these factors:
To be safe, synchronize as much as possible. This makes the program slower, at worst no better than its single-threaded version.
For performance, synchronize as little as possible.
Multithreaded design is a continual tradeoff between these two factors.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 1139
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved