What is real purpose of this wait-notify thread semantics?

后端 未结 2 1658
难免孤独
难免孤独 2021-01-28 17:41

I just came across some code which uses wait-notify construct to communicate with thread defined in a class, by its other member-methods. Amusingly, after acquiring lock, all t

2条回答
  •  耶瑟儿~
    2021-01-28 17:52

    after acquiring lock, all thread does in synchronized scope is timed-wait on same lock (see below snippet).

    Yes, the pattern is strange. Typically I have a loop similar to that (although I always use a private final lockObject) that waits for a small amount of time because I don't want the method to spin -- performing its task too often.

    I would have thought that the other method would lock on the same variable and then update the isShuttingDown flag. But doing the other // useful# sections is a strange pattern since there are a number of race conditions with the code that is going to make determining the order of the useful sections impossible.

提交回复
热议问题