96: Multithreading. Thoughtful Designs.
Take Up Code - En podcast af Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Kategorier:
The design decisions you make affect not only how well you can maintain your code but also how well others can use your code. Multithreading adds a new dimension to your designs and I’ll give you some of my thoughts in this episode. The example class used in this episode is called TotalsTen and consists of three properties: left -> an integer property value that can be read and written. right -> an integer property value that can be read and written. perfectScore -> a bool property value that can be read. The idea behind this class is that the perfectScore property will be true only when the left and right values add up to the value ten. This class sets us up for a race condition and makes it difficult to fix due to the design. I explain how to improve this design while still keeping the original purpose of the class intact. Listen to the full episode or you can also read the full transcript below. Transcript Managing multiple threads takes a lot of thought and in order to do this effectively, you really need to know how the threads are being used. This presents a problem for developers writing code designed to be used in many different places. Many times, the code you write will be at the wrong level of abstraction to deal with threads properly. What do I mean by this? Let’s say you have a class called TotalsTen that has two integer properties called left and right that can be read and changed independently of each other. The TotalsTen class has another property called perfectScore which will be true only when the left and right properties add up to ten. I have no idea what possible purpose a class like this could serve except that it seems like a great example to use for this episode because it’s simple and doesn’t need any special diagrams to understand. It’s just a class with a left int and a right int that when they add up to the value ten, cause another perfectScore property to be true. Any other values that don’t add up to the value ten will cause the perfectScore property to be false. You can’t set the perfectScore property yourself. It gets its value depending on the sum of the left and right properties. Alright, now that we have this highly made-up class, what’s it have to do with multithreading? A class like this is a utility class. It’s designed to perform some function which contributes in some way to an application. This is code that will be used inside an application. So let me ask you. Does this application need to be multithread aware? I have no idea and it’s not possible to know all the needs of all the applications that may want to use your utility class. If you make the class multithread safe and it’s then used in a single threaded application, then not only does your extra work add no value, it actually slows down the application. And if you don’t make your utility class thread safe, then you’re pushing that work onto the application developer. There is no perfect answer to this problem. But I can give you some advice to help make it better right after this message from our sponsor. ( Message from Sponsor ) First of all, this TotalsTen class as it’s currently designed is just asking for race conditions. Check out episode 94 for more information about race conditions. Let’s say that the current values of left and right are 1 and 9. They add up to 10 so the perfectScore property is true. And let’s say there’s a thread that wants to change the values to 3 and 7. Right after this other thread changes left to be 3, your thread comes along and reads the values 3 and 9 which means the perfectScore is false. A moment later, the other thread finishes changing the right value from 9 to 7 which brings the perfectScore back to true. If we wanted to make this class thread safe, it would be difficult with the two separate property setters. You can go way back to episode 31 to learn more about getters and sette