Tuesday 16 February 2021

Part 2 : Shared Resources in threading

#3 minutes of reading ( if images are not visible properly click on image to open in better quality)
In threading it is important to protect shared resources, will see below what happened if shared resource isn’t protected from concurrent access in multithreaded program. Result, the output or behavior of program can be inconsistent.
Here totalEmployee variable is shared resource.
  The output of program is consistent, now let’s call EmployeeCount method with two different threads shown below.
Run application three or four times and you observe output of program different than it is previous, the reason two thread sharing common resource(totalEmployee). There are several ways to restrict concurrent access while accessing shared resources.

Join Method: Join method wait till current thread finishes execution using Join method concurrent access can be avoided shown below.
Lock Keyword: Lock keyword also used to restrict concurrent access
InterLock: It is also used to restrict concurrent access shown below.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home