Which of the following is a valid synchronized method declaration?

  • public synchronized void myMethod() {}
  • public void myMethod() { synchronized() {} }
  • public void synchronized myMethod() {}
  • synchronized void myMethod() {}
The correct syntax for a synchronized method declaration in Java is public synchronized void myMethod() {}, where the synchronized keyword comes before the access modifier (public) and the return type (void). This ensures that only one thread can execute the myMethod at a time.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *