9. 不要在 Java 中的同步块上的非 final 字段上进行同步。 因为对非 final 字段的引用可能随时更改,然后不同的线程可能会在不同的对象上进行同步,即完全没有同步。
10. 不要使用 String 字面值,因为它们可能在应用程序中的其他地方被引用,并且可能导致死锁。 使用`new`关键字创建的字符串对象可以安全使用。 但是,作为最佳实践,请在我们要保护的共享变量本身上创建一个新的**私有**范围内的`Object`实例 OR 锁。 [感谢 Anu 在评论中指出这一点。]
10. 不要使用 String 字面值,因为它们可能在应用程序中的其他地方被引用,并且可能导致死锁。 使用`new`关键字创建的字符串对象可以安全使用。 但是,作为最佳实践,请在我们要保护的共享变量本身上创建一个新的**私有**范围内的`Object`实例 OR 锁。(感谢 Anu 在评论中指出这一点。)
[**Java 并发**](https://howtodoinjava.com/java-concurrency-tutorial/"multithreading in java") 是一个非常复杂的主题,在编写处理在任何给定时间访问一个/多个共享资源的多个线程的应用程序代码时,需要引起很多关注。 Java 5 引入了诸如 [**BlockingQueue**](https://howtodoinjava.com/java/multi-threading/how-to-use-blockingqueue-and-threadpoolexecutor-in-java/"How to use BlockingQueue and ThreadPoolExecutor in java") 和 **Executors**之类的类,它们通过提供易于使用的 API 消除了一些复杂性。
[**Java 并发**](https://howtodoinjava.com/java-concurrency-tutorial/"multithreading in java")是一个非常复杂的主题,在编写处理在任何给定时间访问一个/多个共享资源的多个线程的应用程序代码时,需要引起很多关注。 Java 5 引入了诸如[**`BlockingQueue`**](https://howtodoinjava.com/java/multi-threading/how-to-use-blockingqueue-and-threadpoolexecutor-in-java/"How to use BlockingQueue and ThreadPoolExecutor in java")和`Executors`之类的类,它们通过提供易于使用的 API 消除了一些复杂性。
In general, a thread that uses the `wait()` method confirms that a condition does not exist (typically by checking a variable) and then calls the `wait()` method. When another thread establishes the condition (typically by setting the same variable), it calls the `notify()` method. The wait-and-notify mechanism does not specify what the specific condition/ variable value is. It is on developer’s hand to specify the condition to be checked before calling `wait()` or `notify()`.