Untangle Your Threads
Vlad Patryshev
(this is not me!!!)
© 2020 Vlad Patryshev
Some History
Many years ago Edsger Dijkstra invented semaphores and wrote "Structured Programming". Then C.A.R. Hoare wrote a book, "Communicating Sequential Processes".
Ideas of these two became the foundation of all later books on how processes should communicate.
We find code samples in such books, translated into C, Java, Python, Perl, Ruby, and use them to solve all our problems. Thanks to these guys, all our problems look like a nail now.
This is called Cargo Cult.
Cargo Cult
During WWII, somewhere on Pacific, large amounts of materials were brought in by the combatants, and this was observed by the residents. When the war ended, the flow of goods and materials ceased.
To attract further deliveries of goods, the residents started building imitation landing strips and aircrafts, to attract other aircrafts. /from Wikipedia/
The Problem
Before Refactoring
Recorder Thread | Reader Thread | Reporter Thread | Device Thread | Policy Thread |
|
|
|
|
|
Look Closer
Recorder Thread | Reader Thread | Reporter Thread | Device Thread | Policy Thread |
|
|
|
|
|
Look Closer
Recorder Thread | Reader Thread | Reporter Thread | Device Thread | Policy Thread |
|
|
|
|
|
Why not queue fixes in the listener? Listener also knows when it is enough
Now what
Recorder Thread | Reader Thread | Reporter Thread | Device Thread | Policy Thread |
|
|
|
|
|
Next Step - No Reader Thread
Recorder/Listener Thread | | Reporter Thread | Device Thread | Policy Thread |
| |
|
|
|
Recap
Recorder/Listener Thread | Reporter Thread | Device Thread | Policy Thread |
|
|
|
|
We have no control over this thread
What Sync Do We Have?
Recorder/Listener Thread | Reporter Thread | Device Thread | Policy Thread |
Noticed two sleep points? When Policy Thread wakes up Recorder, we may be in the wrong place |
|
|
|
We have no control over this thread
Make It Simpler
Recorder/Listener Thread | Reporter Thread | Device Thread | Policy Thread |
|
|
|
|
We have no control over this thread
Enter TimerTask
Recorder Task | Reporter Thread | Device Thread | Policy Thread |
|
|
|
|
We have no control over this thread
And Another TimerTask
Recorder Task | Reporter Thread | Device Thread | Policy Task |
|
|
|
|
We have no control over this thread
Reorder by Hierarchy
| Recorder Task | Reporter Thread | Device Thread |
|
|
|
|
We have no control over this thread
Control Task |
|
Controls reporting service
Controls individual reports
Autonomously sends reports
Reorder by Hierarchy
| Recorder Task (Model) | Reporter Thread (View) | Device Thread |
|
|
|
|
We have no control over this thread
Control Task (Controller) |
|
Controls reporting service
Deals with device, builds reports
Just sends reports
Look,
no synchronization!
References
Steve McConnell, "Cargo Cult Engineering", http://www.stevemcconnell.com/ieeesoftware/eic10.htm
C.A.R.Hoare, "Communicating Sequential Processes", http://www.usingcsp.com/
Dijkstra, Dahl, Hoare, "Structured Programming",
http://www.amazon.com/s?search-alias=stripbooks&field-isbn=0122005503
Thank you!
Any questions, really?