1

I'm doing an online quiz related to monitors. Those are the 4 true or false questions and my answers. If my answers are wrong, can you please explain why?

  1. Starvation will definitely not occur when using monitors in Java if notifyAll() is used T

  2. Only objects that are declared to extend thread or implement runnable have a monitor lock in Java F

Sunil
  • 3,404
  • 10
  • 23
  • 31

1 Answers1

1

Starvation will definitely not occur when using monitors in Java if notifyAll() is used. F

If a thread is not granted CPU time because other threads grab it all, it is called "starvation". Check here for more.

  • When using notifyAll(), JVM awakens all threads and then all threads race for the lock on this object. Now, CPU scheduler selects a thread which acquires lock on this object. This means using notifyAll() can not avoid starvation since some threads may always lose the contention.
  • Also, Threads are blocked indefinately waiting to enter a synchronized block, because other threads are constantly allowed access before it. Starvation will also occur.

Only objects that are declared to extend thread or implement runnable have a monitor lock in Java. F

Every class roots from java.lang.Object has monitor lock. Check here for more.

xingbin
  • 27,410
  • 9
  • 53
  • 103