Java Questions 1 - 10   «Prev  Next»

Java Synchronized Keyword Questions

  1. Will a static synchronized method and a non-static synchronized method ever block each other?

    Answer:
    No. The static method locks on a Class instance while the non-static method locks on the this instance.

  2. Does the wait() method give up a lock?

    Answer:
    Yes

  3. When do you need to synchronize to protect data?

    Answer:
    Anytime more than one thread is accessing mutable data. This is to make sure that 2 threads are not changing the mutable data at the same time.

  4. What is the purpose of synchronization?

    Answer:
    Threads working with or operating on the same data need to operate on the data one at a time.

  5. How would you access changeable data in a static field?

    Answer:
    You would use a static method to access it.

  6. What happens when you have a static method access a non-static field and you synchronize the method?

    Answer:
    You acquire a lock on the Class Object.


  7. Why can a static synchronized method and a non-static synchronized method run at the same time?

    Answer:
    In Java, synchronization is a means to control access to resources by multiple threads. When a method is declared as `synchronized`, the method is locked by the thread executing it, which prevents other threads from entering any other `synchronized` method that requires the same lock. A static synchronized method and a non-static synchronized method can run simultaneously because they lock on different objects. A static synchronized method locks on the class object associated with the class (`Class` object). In contrast, a non-static synchronized method locks on the instance of the object the method belongs to (the `this` reference).
    Since the class-level lock is distinct from the object-level lock, a thread accessing a static synchronized method cannot interfere with threads accessing non-static synchronized methods of an instance of that class, and vice versa. This separation allows for concurrent execution of static and instance synchronized methods, which is by design to allow for finer-grained control over concurrency in Java. This behavior is particularly beneficial when you have to synchronize access to static data (class-level data) and instance data (object-level data) separately. It allows threads to concurrently access class-level data and instance-level data as long as they are not trying to access the same type of synchronized method (static or non-static) on the same class or instance.
    In a nutshell, because they will not block each other.

  8. What must you do in order to keep a class thread safe?

    Answer:
    Methods that access changeable fields must be synchronized.

  9. What generalization can you make regarding static fields and methods?

    Answer:
    Access to static fields should be done from static synchronized methods. Access to non-static fields should be done from non-static synchronized methods.

  10. How would you describe a class that is thread safe?

    Answer:
    When a class has been synchronized to protect its data, we say that the class is thread-safe

SEMrush Software