Reading shared variable from another thread (Effective Java #66)

前端 未结 4 561
借酒劲吻你
借酒劲吻你 2021-01-05 09:13

In Effective Java: item 66, Joshua Bloch gave an example about life failure:

// Broken! - How long would you expect this program to run
class StopTh         


        
4条回答
  •  不思量自难忘°
    2021-01-05 09:45

    System.out.println() requests a resource to write on the console, which in itself a blocking method… meaning, it will block the backgroundThread() to print() on the console. This is similar to sending an interrupt to it.

    Thus, backgroundThread() will become aware of the change in the value of the boolean and stop executing, thereby terminating the Daemon.

提交回复
热议问题