官术网_书友最值得收藏!

Low coupling

The code dependency between threads is referred to as coupling. We should try to keep coupling as low as possible, to avoid complexity and make the code base easy to read and maintain. Now, what does that actually mean? Refer to the program with threading where we accessed and modified the someData value from two threads simultaneously. That can be called coupling, as both the threads were dependent on each other. For your reference, we copied the following code-snippet:

async(CommonPool) { 
        for(i in 11..20) { 
            myData.someData+=i 
            println("someData from 1st async ${myData.someData}") 
            delay(500) 
        } 
    } 
 
    async(CommonPool) { 
        for(i in 1..10) { 
            myData.someData++ 
            println("someData from 2nd async ${myData.someData}") 
            delay(300) 
        } 
    } 

In the next program, where we introduced immutability, the coupling is reduced. Here, both the threads were reading the same element, but one thread's operations and changes didn't affect the other one.

主站蜘蛛池模板: 策勒县| 溧水县| 鱼台县| 河南省| 铜陵市| 许昌县| 瑞金市| 海晏县| 武平县| 宕昌县| 泗阳县| 宣汉县| 绥芬河市| 卢湾区| 即墨市| 永德县| 合水县| 大安市| 海林市| 昔阳县| 山阳县| 云南省| 鄯善县| 甘洛县| 永顺县| 信宜市| 宁阳县| 灵璧县| 金秀| 改则县| 宝兴县| 资溪县| 淳安县| 浑源县| 棋牌| 武宁县| 云浮市| 镇赉县| 子长县| 全州县| 新巴尔虎左旗|