Problems with synchronized keyword in java

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

Leave a Comment