Using the synchronized keyword in Java can cause the following problems.
- We are not having any flexibility to try for a lock without waiting.
- There is no way to specify a maximum waiting time for a Thread to get a lock so that Thread will wait until getting the lock which may create a performance problem, this can also cause get a lock.
- If a Thread releases a lock then which waiting for Thread will get the lock, we are not having any control over this.
- We can not list out all waiting Thread for a lock.
- It is compulsory that we have to use synchronized keywords either at the method level or within a method. It is not possible to use synchronized keywords across multiple methods.
To overcome these problems import the java. util. concurrent.locks package introduced in Java 1.5v. It also provides several enhancements to the programmer for more control and concurrency.
Similar Java Tutorials
- Odd Even in Java
- Exception Hierarchy in Java
- Java Exception Handling Interview Questions
- final finally finalize in java
- User Defined Exception in Java
- Exception Handling Keyword in Java
- throw throws in Java
- try with multiple catch block in Java
- Threads in Java
- Thread priority in Java
- Threads communication in Java
- Daemon Thread in Java