Which two demonstrate the valid usage of the keyword synchronized?

Which two demonstrate the valid usage of the keyword synchronized?

Which two demonstrate the valid usage of the keyword synchronized?

A.
interface ThreadSafe {
synchronized void doIt();
}

B.
abstract class ThreadSafe {
synchronized abstract void doIt();
}

C.
class ThreadSafe {
synchronized static void soIt () {}
}

D.
enum ThreadSafe {
ONE, TWO, Three;
Synchronized final void doIt () {}
}

Explanation:
The Java programming language provides two basic synchronization idioms:
synchronized methods and synchronized statements.
To make a method synchronized, simply add the synchronized keyword to its declaration.



Leave a Reply 6

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


Francesco

Francesco

The correct answers are C and D

Ray

Ray

C D

Ray

Ray

Illegal modifier for the interface method doIt; only public & abstract are permitted.
The abstract method doIt in type thread can only set a visibility modifier, one of public or protected.