why this synchronized method is not working as expected?

前端 未结 2 1372
庸人自扰
庸人自扰 2020-12-11 09:58

Could someone explain two me why these to codes dont output the same results (the only difference between two codes is in the run() method) ?

NB: the first code seem

2条回答
  •  无人及你
    2020-12-11 10:44

    When you want to synchronize two threads you must lock on a shared resource by all threads. Synchronizing in the run method(or any instance method in thread class) each thread locks it's own method resulting no synchronization at all.

提交回复
热议问题