Java Programming - Threads - Discussion

3. 

Which three are methods of the Object class?

  1. notify();
  2. notifyAll();
  3. isInterrupted();
  4. synchronized();
  5. interrupt();
  6. wait(long msecs);
  7. sleep(long msecs);
  8. yield();

[A]. 1, 2, 4
[B]. 2, 4, 5
[C]. 1, 2, 6
[D]. 2, 3, 4

Answer: Option C

Explanation:

(1), (2), and (6) are correct. They are all related to the list of threads waiting on the specified object.

(3), (5), (7), and (8) are incorrect answers. The methods isInterrupted() and interrupt() are instance methods of Thread.

The methods sleep() and yield() are static methods of Thread.

D is incorrect because synchronized is a keyword and the synchronized() construct is part of the Java language.


Nan said: (Jun 10, 2015)  
What notify do?

Anand Yadav said: (Jul 25, 2015)  
Notify is used to wake the thread which is in waiting state.

Aarti Gupta said: (Mar 1, 2016)  
I don't get it for synchronized()

Aarti Gupta said: (Mar 1, 2016)  
The java.lang.Object.notify() wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.

Kartiki said: (Sep 6, 2018)  
Anyone explain in detail?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.