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

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.

主站蜘蛛池模板: 沙湾县| 新河县| 文山县| 娱乐| 荔浦县| 嘉黎县| 遵义县| 都兰县| 辽阳县| 砀山县| 建德市| 区。| 明水县| 都江堰市| 洛浦县| 惠来县| 临颍县| 葫芦岛市| 五常市| 枣阳市| 赫章县| 临夏市| 饶阳县| 佛教| 板桥市| 惠来县| 新野县| 内黄县| 延吉市| 濮阳市| 绥芬河市| 五原县| 正宁县| 济南市| 天台县| 新乐市| 溆浦县| 武邑县| 新乡县| 扎囊县| 乌拉特后旗|