Java Questions 11 - 20  «Prev  Next»

Java Threads and Synchronization Interview Questions

  1. What is the difference between StringBuffer and Stringuilder?

    Answer:
    All the methods in StringBuffer are synchronized when necessary, while those in StringBuilder are not.
    java.lang 
    Class StringBuffer
    
    java.lang.Object
    extended by java.lang.StringBuffer
    All Implemented Interfaces:
    Serializable, Appendable, CharSequence
    

    StringBuffer class is safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.

  2. What is the purpose of the synchronizedList?

    Answer:
    The method Collections.synchronizedList() returns a List whose methods are all
    1)synchronized and 2) thread-safe according to the documentation.

  3. What is true about the methods returned by the class synchronizedList()?

    Answer:
    In a "thread-safe" class like the one returned by sychronizedList(), each individual method is synchronized.

  4. In Java SE 17, is the only way to synchronize a method to place the "synchronized" keyword in front of the method?

    Answer:
    In Java SE 17, while placing the `synchronized` keyword before the method is the most common way to synchronize it, it's not the **only** way. Here are the different methods for synchronizing a method in Java SE 17:
    1. Synchronized keyword: This is the traditional and most commonly used approach. Placing the `synchronized` keyword before the method declaration makes the entire method execution thread-safe. Only one thread can execute the synchronized method at a time, ensuring data consistency and preventing race conditions.
    2. Synchronized blocks: You can also use synchronized blocks within non-synchronized methods to control specific sections of code that need to be thread-safe. The syntax involves using the `synchronized` keyword with an object as the lock:
      public void someMethod() {
        synchronized (this) {
          // This block is synchronized on the current object
          // Only one thread can access this block at a time
        }
      
      
    3. Synchronized static methods: Java SE 17 introduced the ability to make static methods synchronized. This can be useful when you need to synchronize access to class-level data or perform static operations that require mutual exclusion.
    4. `ReentrantLock` and other concurrency classes: The Java concurrency API provides various tools like `ReentrantLock` and `Semaphore` for more fine-grained control over synchronization. These classes offer greater flexibility compared to the basic synchronized keyword but require more complex code implementation.
    5. `synchronized` keyword with collections: Certain collection classes like `HashMap` offer optional `synchronized` versions that provide basic thread-safety for individual operations. However, it's important to note that these collections might not be fully thread-safe and require additional synchronization depending on your use case.

    Choosing the right synchronization method: The best way to synchronize a method depends on your specific needs and the level of granularity required. In many cases, the simple `synchronized` keyword before the method declaration is sufficient. However, for more complex scenarios or fine-grained control, using synchronized blocks, static synchronized methods, or the concurrency API might be necessary. Remember, synchronization adds overhead and can impact performance. Use it judiciously and only where necessary to ensure thread safety and data consistency.


  5. What is Thread Deadlock?

    Answer:
    Deadlock occurs when 2 threads are blocked, with each waiting for the other's lock. Neither can run until the other gives up its lock, so they will sit forever.
    Deadlock describes a situation where two or more threads are blocked forever, waiting for each other.
    For example, two threads T1 and T2 need a File and a Printer. T1 acquires the lock for the file and is about to acquire the lock for the Printer but before it could acquire the lock, T2 acquires the lock for the Printer and tries to acquire the lock for the file (which is already held by T1). So now, both the threads keep waiting for ever for each other to release their locks and neither will be able to proceed.

  6. What is the purpose of a) wait b) notify() and c) notifyAll()?

    Answer:
    Java concurrency model uses locks to implement mutually exclusive access to objects in a multithreaded environment.
    Locks are associated with every object in Java.
    wait(), notify(), and notifyAll() methods are used by threads to communicate with each other while attempting to access a common object.

  7. What must you take into consideration when using the methods wait(), notify(), and notifyAll()?

    Answer:
    They must be called from within a synchronized method or block. A thread cannot invoke methods wait or notify on an object unless it owns that object's lock.

  8. To which class do wait() and notify() belong to?

    Answer:
    These 2 methods belong to the Object class.

  9. How does a thread get on the waiting list?

    Answer:
    A thread gets on the waiting list by executing the wait() method of the target object.

  10. What must be true for a thread to call
    1. wait() or
    2. notify ()?

    Answer:
    The thread has to be the owner of the lock for that object.

SEMrush Software