synchronized thread execution

后端 未结 4 974
终归单人心
终归单人心 2021-01-22 17:27

my task is to create thread in this order: if A start->start B and C, if B start->start D. And destroy them in reverse order If D then B. If B and C then A. I hope you get it.

4条回答
  •  误落风尘
    2021-01-22 17:51

    In my opinion, it is quite strange to use synchronized (lock) in your run method to lock your object. The reason is that in each Thread object has the different lock attribute, which is belong to each object. It means you are trying to lock the different objects. Actually, it doesn't make sense.

    Basically, the object that you should apply the synchronized are any shared objects. For example, you need to count something and then you create a class object to share it in your class. In this case, it should be locked while being read or written.

提交回复
热议问题