Changes for page Concurrency

Last modified by chrisby on 2024/06/02 15:15

From version 1.20
edited by chrisby
on 2023/11/30 21:27
Change comment: There is no comment for this version
To version 1.3
edited by chrisby
on 2023/11/26 19:47
Change comment: There is no comment for this version

Summary

Details

Page properties
Title
... ... @@ -1,1 +1,1 @@
1 -Concurrency
1 +Concurrency (todo)
Content
... ... @@ -1,52 +1,30 @@
1 -Objects are abstractions of processing, **threads are abstractions of timing**.
1 +* When I/O is the bottleneck of your application, more threads will increase the performance in opposite to when CPU is the bottleneck.
2 +* Stress Testing: Checking the throughput of an application by sending a huge amount of requests and examining the response times.
3 +* Isolate concurrent code by putting it in a few separate classes.
4 +* Always consider the concept of execution paths: The amount of possible interleaving of instructions that are processed by at least two threads. For example, objects with mutable states could unintentionally cause different results doing the same operation twice.
5 +* Atomic operation = operation which can not be interrupted by other threads. But for unsynchronized processes threads can put instructions between two atomic operations.
6 +* `synchronized` prevents unintended side-effects.
7 +* Server-based locking is preferred over client-based locking.
8 + * Server-based locking: The class used takes care of internal locking, so the user has nothing else to worry about.
9 + * Client-based locking: User has to manually implement locking. This approach error prone and hard to maintain.
10 +* If there is no access to the server an adapter class can be used instead. Even better would be thread-save collections using extended interfaces.
11 +* As little synchronized code (`synchronized`) as possible should be used. And if, then only for small, critical code sections.
2 2  
3 -### Why Concurrency?
13 +### Problems of testing multi-threaded methods
4 4  
5 -* **Concurrency is a decoupling strategy**. The what is decoupled from the when.
6 -* **Concurrency is can improve the throughput and structure** of an application.
15 +* Very tricky which is why concurrency should be avoided in the first place.
16 +* In general this requires a lot of iteration which makes it resource intensive.
17 +* The outcome is architecture dependent (OS, hardware) which introduced randomness and make the error detection unreliable.
7 7  
8 -### Why Not Concurrency?
19 +### Solution approaches for testing multi-threaded methods
9 9  
10 -* **Unclean**: It is hard to write clean concurrent code, and it is harder to test and debug.
11 -* **Design Changes**: Concurrency doesn't always improve performance behavior and but it always requires fundamental design changes.
12 -* **Extra Management**: Concurrency demands a certain amount of management effort, which degrades performance behavior and requires additional code.
13 -* **Complexity**: Proper concurrency is complex, even for simple problems.
14 -* **Unreproducible**: Concurrency bugs are usually not reproducible; therefore, they are often written off as one-time occurrences (cosmic rays, glitches, etc.) rather than treated as true defects, as they should be.
15 -* **Side-Effects**: When threads access out-of-sync data, incorrect results may be returned.
16 -
17 -### Defensive Concurrency Programming
18 -
19 -* **Single-Responsibility Principle**
20 - * **Separation of code**: Changes to concurrent code should not be mixed with changes to the rest of the code. So you should separate the two cleanly.
21 - * **Separation of change**: Concurrent code has special problems that are different, and often more serious, than sequential code. This means that concurrent and sequential code should be changed separately, not within the same commit, or even within the same branch.
22 -* **Principle of Least Privilege**: Limit concurrent code to the resources it actually needs to avoid side effects. Minimize the amount of shared resources. Divide code blocks and resources into smaller blocks to apply more granular, and therefore more restrictive, resource access.
23 -* **Data Copies**: You can sometimes avoid shared resources by either working with copies of data and treating them as read-only, or by making multiple copies of the data, having multiple threads compute results on them, and merging those results into a single thread. It is often worth creating multiple objects to avoid concurrency problems.
24 -* **Independence**: Threads should be as independent as possible. Threads should not share their data or know anything about each other. Instead, they should prefer to work with their own local variables. Try to break data into independent subsets that can be processed by independent threads, possibly in different processes.
25 -
26 -### Basic Knowledge
27 -
28 -Before starting to write concurrent code, get familiar with the following basics:
29 -
30 -* **Libraries**: Use the thread-safe collections provided. Use non-blocking solutions if possible. Be aware multiple library classes are not thread safe.
31 -* **Concepts**: Mutual Exclusion, Deadlock, Livelock, Thread Pools, Semaphore, Locks, Race Condition, Starvation,
32 -* **Patterns**: Producer-consumer, Reader-Writer
33 -* **Algorithms**: Study common algorithms and their use in solutions. For example, the Dining Philosophers problem.
34 -
35 -### Synchronized Methods
36 -
37 -Synchronized means that only one thread can access a method at a time to prevent side effects.
38 -
39 -* **Avoid dependencies between synchronized methods**: In concurrent code, such dependencies, such as when one synchronized method calls another, can cause subtle bugs like deadlocks and performance issues.
40 -* **Avoid applying more than one method to a shared object.** If this is not possible, you have three options:
41 - * **Client-based locking**: The client locks the server, calls all the server methods, and then releases the lock.
42 - * **Server-based locking**: Create a method in the server that locks the server, calls all the methods, and then unlocks the server. A client can now safely call this new method.
43 - * **Adapted Server**: Create an intermediate component to perform the lock. This is a variant of server-based locking when the original server cannot be changed. Ideally, one would use thread-save collections and implements them behind extended interfaces.
44 -* **Server-based locking is preferred over client-based locking.** With server-based locking, the class used takes care of the internal locking, so the user has nothing else to worry about. With client-based locking, the user has to implement locking manually, which makes the approach error-prone and difficult to maintain.
45 -* **Keep synchronized sections small.** Locks are expensive because they add administrative overhead and delay. On the other hand, critical sections need to be protected. Critical sections are pieces of code that will only run correctly if they are not accessed by multiple threads at the same time. Keeping synchronized sections small avoids both problem. Only use it for small, critical code sections.
46 -
47 -### Miscellaneous
48 -
49 -* **Performance**: When a performance bottleneck is detected in an application, the cause can be I/O or CPU. Increasing the number of threads will show which of the two is the actual bottleneck, see [[this article|doc:Software Engineering.Testing.Enhance Test Execution Speed.WebHome]].
50 -* **Stress Testing**: A common type of test that determines the maximum throughput of an application by sending a large number of requests and examining the response times.
51 -* **Execution Paths**: Always consider the concept of different execution paths. The amount of possible interleaving of instructions that are processed by at least two threads. For example, objects with mutable states could unintentionally cause different results doing the same operation twice.
52 -* **Write Shutdown Code Early**: Shutting down an application requires the safe termination of all concurrent processes. Writing shutdown code is difficult. Writing shutdown code early is cheaper than writing it later. Study the available algorithms.
21 +* Monte-Carlo-Tests
22 + * Write flexible, adaptive tests.
23 + * Repeatedly run them on a test server and randomly vary the test settings.
24 + * If something fails, the code is defect and the applied settings should be logged.
25 + * Do this early to gain tests ASAP for your testing repertoire or CI-server.
26 +* Execute these tests on every platform over a long time to increase the probability that
27 + * the production code is correct or
28 + * the test code is bad.
29 +* Execute the tests on a computer using simulations of application loads when possible.
30 +* There are tools to test thread-based code like ConTest.