Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

The ThreadStaticAttribute Class

c



+ Font mai mare | - Font mai mic



The ThreadStaticAttribute Class

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;
using System.Threading;

namespace WroxStatic




public class MainApp



The output from WroxStatic is:

i = 1 ThreadID = 2 x(static attribute)= 1 y = 2
i = 1 ThreadID = 3 x(static attribute)= 1 y = 3
i = 2 ThreadID = 2 x(static attribute)= 2 y = 4
i = 2 ThreadID = 3 x(static attribute)= 2 y = 5
i = 3 ThreadID = 2 x(static attribute)= 3 y = 6
i = 3 ThreadID = 3 x(static attribute)= 3 y = 7
i = 4 ThreadID = 2 x(static attribute)= 4 y = 8
i = 4 ThreadID = 3 x(static attribute)= 4 y = 9
i = 5 ThreadID = 2 x(static attribute)= 5 y = 10
i = 5 ThreadID = 3 x(static attribute)= 5 y = 11
i = 6 ThreadID = 2 x(static attribute)= 6 y = 12
i = 6 ThreadID = 3 x(static attribute)= 6 y = 13
i = 7 ThreadID = 2 x(static attribute)= 7 y = 14
i = 7 ThreadID = 3 x(static attribute)= 7 y = 15
i = 8 ThreadID = 2 x(static attribute)= 8 y = 16
i = 8 ThreadID = 3 x(static attribute)= 8 y = 17
i = 9 ThreadID = 2 x(static attribute)= 9 y = 18
i = 9 ThreadID = 3 x(static attribute)= 9 y = 19
i = 10 ThreadID = 2 x(static attribute)= 10 y = 20
i = 10 ThreadID = 3 x(static attribute)= 10 y = 21

We 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 and Performance

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



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 1061
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved