Java Questions 1 - 10   «Prev  Next»

Java Thread Synchronization Questions

  1. Can a class have both synchronized and non-synchronized methods?

    Answer:
    Yes

  2. When you syncrhonize a method, which object must be acquired?

    Answer:
    When you synchronize a method, the object used to invoke the method is the object whose lock must be acquired.

  3. Whose lock is used to protect the static methods of the class (if they are synchronized)?

    Answer:
    It is the java.lang.Class instance whose lock is used to protect the static method of the class (if they are synchronized) .

  4. What does the synchronized keyword enable you to achieve with threads?

    Answer:
    The Java programming language provides two basic synchronization idioms: 1) synchronized methods and 2) synchronized blocks.
    The keyword synchronized designates methods and blocks of code for safe, serialized access within an object.

  5. What is the function of a .class appended at the end of a Java class?

    Answer:
    That is called a class literal. It is a special feature in the Java language that tells the compiler (which tells the JVM) go and find me the instance of Class that represents the class called MyClass.

  6. What is the purpose of getting the lock on an object?

    Answer:
    So that other threads cannot modify the object while the block of code is executing.

  7. What is the difference between a StringBuffer and String object?

    Answer:
    In Java, `StringBuffer` and `String` objects are used to handle text data, but they serve different purposes and have distinct characteristics.
    1. Immutability:
      • String: A `String` object is immutable, meaning that once it is created, its value cannot be changed. Any modification to a `String` object results in the creation of a new `String` object. This immutability makes `String` safe for use in multi-threaded environments without the need for synchronization.
      • StringBuffer: A `StringBuffer` object, on the other hand, is mutable. It can be modified after it is created, allowing for changes to the characters within the buffer or the length of the sequence. This makes `StringBuffer` more memory efficient when performing numerous modifications to a sequence of characters.
    2. Performance:
      • String: Due to the immutable nature of `String` objects, operations that alter the content, such as concatenation, can be less efficient in terms of memory usage, because each operation results in the creation of a new `String` object.
      • StringBuffer: `StringBuffer` provides a way to reduce this overhead by allowing modifications to the existing object itself. This can lead to better performance when handling large amounts of text data that needs to be modified frequently.
    3. Synchronization:
      • String: Since `String` objects are immutable, there's no need for synchronization.
      • StringBuffer: `StringBuffer` includes synchronized methods to ensure thread safety, meaning that operations on a `StringBuffer` are safe from concurrent access by multiple threads. However, this synchronization can introduce a performance cost in single-threaded scenarios or when thread safety is not a concern.
    4. Use Cases:
      • String: Given its immutability and inherent thread safety, `String` is suitable for values that are not expected to change, such as constants, configuration values, or any scenario where text data remains constant.
      • StringBuffer: It is ideal for text data that needs to be altered frequently, such as in loops, when constructing dynamic SQL queries, or when performing complex text processing that involves numerous modifications.

    In summary, the choice between `StringBuffer` and `String` depends on the specific requirements of your application, including considerations of thread safety, performance, and the mutability of the text data you are working with. For single-threaded applications where thread safety is not a concern, and mutability is desired, `StringBuilder` (a similar class to `StringBuffer` but without synchronized methods) might be a more suitable alternative due to its higher performance. Strings cannot be modified once they are created and they are immutable and the String Class is final.


  8. What do you call the condition when a thread tries to enter a synchronized method and the lock is already taken?

    Answer:
    The thread is said to block on the object's lock.

  9. If a lock is released, does this mean that a thread will get the object?

    Answer:
    No. Just because a lock is released does not mean that any particular thread will obtain the object.

  10. What is the difference between
    1. Threads calling non-static synchronized methods in the same class
    2. Threads calling static synchronized methods in the same class?
    This is has to do with the definition of static.

    Answer:
    For a) they will only block each other if they are invoked using the same instance.
    For b), these threads will always block each other.

SEMrush Software