• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/10

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

10 Cards in this Set

  • Front
  • Back
What is the difference between a synchronized method and a synchronized block?
A synchronized method ensures that the whole method is executed completely by one thread before another thread can invoke it. A synchronized block restricts the mutual exclusion to only that block of code. One should try to make the synchronized block as small as possible.
What is the difference between notify() and notifyAll()?
notify() wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation ( could be priority based ). notifyAll() wakes up all the threads waiting for the lock. They all compete for the lock and the winner gets it. The method of picking the winner is non-deterministic and may not be fair.
What is a deadlock?
Deadlock is a condition when multiple threads are stuck because they are waiting for a resource that is owned by another thread and there is a cyclic pattern of this phenomenon. A simple example is thread t1 owns resource A and is waiting for resource B and thread t2 owns resource B and is waiting for resource A. When this happens, none of the threads can proceed and we encounter a deadlock. This scenario can be extended to multiple threads owning one resource and waiting for another resource that forms a circular dependency. This causes a deadlock.
What four things are required for a deadlock to occur?
1. Mutual exclusion, which is to say that at least one resource is not shareable, i.e. can only be used by one process at a time.
2. Hold and wait which is to say that a process holds at least one resource and requests resources held by other processes.
3. No preemption which is to say that the resource must be voluntarily surrendered by the process.
4. Circular wait which is to say that given a set of processes {P1, P2, P3,...Pn}, P1 has a resource needed by P2, P2 has a resource needed by P3,... Pn has a resource needed by P1.
How do you create a thread in Java?
Two ways to create a thread are:
1. Extend the Thread class and implement the run() method.
2. Implement the run() method of the Runnable interface and pass the resulting object to a plain Thread object.
Why do you need synchronization?
Synchronization is needed when a shared resource is being accessed and modified by different threads. Without Synchronization, the resource may be found in an inconsistent state if one thread gets pre-empted before it can complete it's action, e.g. a node is being added to a tree and another thread tries to traverse the tree.
Explain how to use the join() method.
The method join is a method of the Thread object. It is defined as final void join() throws InterruptedException. When this method is invoked on thread t, the calling thread goes to sleep until thread t terminates. Its name comes from the concept of the calling thread waiting until the specified thread joins it.
Explain the wait() and notify() methods of the Object class.
These methods provide a mechanism for a thread to notify another thread. The thread is said to be waiting on the object in question. When another thread calls the notify() method of the same Java object, this wakes up a waiting object.
What is a livelock?
Livelock is when two threads or processes get deadlocked because they try to perform the same action to come out of a potential deadlock. A real-world example of livelock occurs when two people meet in a corridor and each tries to be polite by moving aside. Problem is they move in the same direction and are still in each other's way.
What are the guidelines for picking up a method to create a thread in Java?
If a class already extends from a base class, it is not possible to also extend the Thread class because multiple inheritance is not supported in Java classes. In that case, the user has the option of implementing Runnable.