Java Questions 11 - 20  «Prev  Next»

static non-static Thread Class Method Questions

  1. Of the key methods in the Thread class, which are static and which are non-static?

    Answer:
    1. sleep() and yield() are static
    2. join() and start() are non-static

  2. Why do wait() and notify() have to be synchronized?

    Answer:
    You almost always need to share some mutable data between threads at the same time, and that means you need synchronization.

  3. When does a thread become a "thread of exeuction"?

    Answer:
    This occurs when the programmer calls the Thread object's start() method.

  4. What happens when an object goes to sleep? In Java SE 17, when an object goes to sleep, does it hold onto any locks it acquired prior to sleeping?

    Answer:
    When an object goes to sleep, it holds onto any locks it acquired prior to sleeping.
    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 are the 5 thread states?

    Answer:
    1. new
    2. runnable
    3. running
    4. blocked/waiting/ sleeping
    5. dead

  6. What happens once a thread is dead?

    Answer:
    It can never be restarted.

  7. What is the only way a thread can transition to "running"?

    Answer:
    The thread must be in the runnable state.

  8. How do you let one thread execute a method, but prevent other threads from running the same object's method.

    Answer:
    In order to achieve this use the synchronized keyword.

  9. How do you coordinate activity between different threads?

    Answer:
    Use
    1. wait
    2. notify
    3. and notifyAll()
    4. methods.

  10. What type of argument does the Thread constructor take?

    Answer:
    The Thread constructor takes a "Runnable" argument.


SEMrush Software