• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
Front

How to study your flashcards.

Right/Left arrow keys: Navigate between flashcards.right arrow keyleft arrow key

Up/Down arrow keys: Flip the card between the front and back.down keyup key

H key: Show hint (3rd side).h key

image

PLAY BUTTON

image

PLAY BUTTON

image

Progress

1/16

Click to flip

16 Cards in this Set

  • Front
  • Back
When a Java object calls wait(), what happens to the mutex lock that it’s holding?
The mutex lock is silently released and the thread sleeps. When another thread acquires the lock and calls notify(), the lock is again silently released and, sometime later, is returned to the waiting thread. It is possible for a third thread to acquire the lock at any time that the lock is released. This behavior is implemented deep within the virtual machine and requires support from the underlying operating system. However, the code can confuse programmers because the lock is actually released for a time even though it is in a synchronized block. Consider the following code that implements a rudimentary blocking queue. (Please note: As of Java 1.5 a blocking queue is supplied in a standard API and you shouldn’t need to code your own in most cases.
What are the two distinct families of pausing execution?
maintains lock: Thread.yield() and Thread.sleep() blocking

releases lock: Object.wait()
What happens if a thread calls wait() on an object it hasn't locked?
IllegalMonitorStateException is thrown. It must be in a synchronized block/method.

http://java.sun.com/javase/6/docs/api/java/lang/Object.html#wait%28%29
What is the difference between yield(), sleep() and wait() ?
The static Thread.sleep(long) method maintains control of thread execution but delays the next action until the time expires.

The wait() method gives up control over thread execution indefinitely so that other threads can run and also releases locks held.

The yield() method returns it to the ready state, either from waiting, running or after its creation. When a task invokes its sleep() method, it returns to the waiting state from a running state.
Are there any guarantees when a thread will execute when it calls yield()?
No, it becomes just another thread in scheduler's set. A sleeping thread is guaranteed only to pause as long as specified but may
pause longer.
Why use a while loop instead of an if statement to check conditions?
The JVM may 'wake up' a waiting thread without it being notified, interrupted or timed out (spurious wakeup). This happens rarely but must be guarded against. Waits should always occur in loops.
Can you rely on thread priorities?
No, scheduling (and yielding) depends on the JVM manufacturer
Can one implement one's own start() method?
The Thread.start() method is not marked final, but should not be overridden. This method contains the code that creates a new executable thread and is very specialized. Your threaded application should either pass a Runnable type to a new Thread, or
Thread and override the run() method.
What is the volatile modifier for?
The volatile modifier is used to identify variables whose values should not be optimized by the JVM, by caching the
value for example. The volatile modifier is typically used for variables that may be accessed or modified by numerous independent
threads and signifies that the value may change without synchronization.
What are different ways in which a thread can enter the waiting state?
* Invoking its sleep() method,
* By blocking on I/O
* By unsuccessfully attempting to acquire an object's lock
* By invoking an object's wait() method.
* It can also enter the waiting state by invoking its (deprecated) suspend() method.
What is mutual exclusion? How can you take care of mutual exclusion using Java threads?
Mutual exclusion is a phenomenon where no two processes can access critical regions of memory at the same time. Simply use the
synchronized keyword and explicitly or implicitly provide an Object, any Object, to synchronize on. This can be applied to a class,
a method, or a block of code. There are several methods in Java used for communicating mutually exclusive threads such as wait(),
notify(), or notifyAll(). For example, the notifyAll() method wakes up all threads that are in the wait list of an object.
What are the states associated in the thread?
A thread is an independent path of execution in a system. The high-level thread states are ready, running, waiting and dead.

* new
* blocked
* runnable
* terminated (dead)
* timed waiting
* waiting
What is daemon thread and which method is used to create the daemon thread?
Daemon threads are threads with low priority and runs in the back ground, such as those doing the garbage collection operation for
the java runtime system. The setDaemon() method is used to create a daemon thread, but only prior to starting. These threads run
without the intervention of the user. To determine if a thread is a daemon thread, use the accessor method isDaemon()
How do you cause a thread to wait for the completion of other threads?
Thread.join()
* forces the calling thread to wait until the called thread completes before proceeding
* can be called with a timeout argument that allows the join() call to return even when the thread isn't finished.
* can be interrupted by calling interrupt, forcing the caller to handle/propogate InterruptionException. note: can create Joiner
threads (one-shot variant) that starts in a blocked/join mode and executes when it is interrupted
What is a deadlock?
TBA
What is a race condition?
TBA